开发者

WPF: How can I get current inheritor ListView from DataTemplate

I have 2 inheritors of ListView:

    public class FileListView : ListView
    public class ThumbnailListView : ListView

In XAML I have following code:

    <Window.Resources>
        <DataTemplate x:Key="FileListViewTemplate">
          <dtc:FileListView/>
        </DataTemplate>
        <DataTemplate x:Key="ThumbnailViewTemplate">
          <dtc:ThumbnailView/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
      <CheckBox x:Name="MyCheckBox">
      <ContentControl x:Name="MyContentControl"开发者_Go百科 MouseDown="OnContentControlMouseDown">
        <DataTemplate>
          <ContentPresenter x:Name="AudioPresenter" 
                            ContentTemplate="{StaticResource FileListTemplate}"/>
          <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding ElementName=MyCheckBox, Path=IsChecked}"
                         Value="true">
              <Setter TargetName="AudioPresenter"
                      Property="ContentTemplate"
                      Value="{StaticResource ThumbnailViewTemplate}" />
            </DataTrigger>
          </DataTemplate.Triggers>
        </DataTemplate>
      </ContentControl>
    </Grid>

In code behind:

     public ListView GetCurrentListView()
    {
        ListView lv = null;
        DataTemplate fileListViewTemplate = base.FindResource("FileListViewTemplate") 
                                            as DataTemplate;
        DataTemplate thumbnailViewTemplate = base.FindResource("ThumbnailViewTemplate") 
                                           as DataTemplate;

        ContentPresenter contentPresenter = VisualTreeHelper.GetChild(this.MyContentControl, 0)
            as ContentPresenter;
        try
        {
            lv = fileListViewTemplate.FindName("FileListView", contentPresenter)
            as ListView;
        }
        catch (Exception)
        {
            lv = thumbnailViewTemplate.FindName("ThumbnailView", contentPresenter)
            as ListView;
        }
        return lv;
     }

If CheckBox is noted, then I want to see ThumbnailView, otherwise I want to see FileListView. And sometime I want to get current list view from code behind. What am I doing wrong?


may be you shoud use converter to convert "true" from string to bool when you specify the Value in your DataTrigger?

    public sealed class StringToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var flag = false;
        if (value is bool)
            flag = (bool)value;
        else
            if (value is bool?)
            {
                var nullable = (bool?)value;
                flag = nullable.GetValueOrDefault();
            }
        return flag;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var back = ((value is String) && (((String)value) == "true"));
        return back;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜