WPF datatemplateselector未被调用

时间:2013-09-29 15:58:45

标签: c# .net wpf datatemplateselector

您好我正在尝试动态更改datatemplate但我的方法TreeViewItemTemplateSelector中的方法SelectTemplate永远不会被调用(我已经通过调试器检查过):(请帮帮我:)

来自xaml MainWindow的代码:

代码背后的代码:

2 个答案:

答案 0 :(得分:0)

将DataTemplates从TreeView.Resources移动到Window.Resources

         <Window.Resources><DataTemplate x:Key="DefaultTemplate">
            <TextBlock Text="{Binding Header}"></TextBlock>
        </DataTemplate><DataTemplate x:Key="Regulation">
            <TextBlock Text="{Binding Path=Header}" FontWeight="Bold"></TextBlock>
        </DataTemplate>

        <DataTemplate x:Key="Article">
            <TextBlock Text="{Binding Path=Header}" Foreground="Green"></TextBlock>
        </DataTemplate>        
<local:TreeViewItemTemplateSelector x:Key="TemplateSelector" DefaultTemplate="{StaticResource DefaultTemplate}"  ArticleTemplate="{StaticResource Article}" RegulationTemplate="{StaticResource Regulation}"  />

并进行更改

 <TreeView ItemTemplateSelector="{StaticResource TemplateSelector}" Height="409" HorizontalAlignment="Left" Margin="10,10,0,0" Name="treeView1" VerticalAlignment="Top" Width="277" ItemsSource="{Binding}"/>

更新代码,我们会看到。我把类似的代码放到VS中,它的工作原理我们需要仔细看看。所以我检查了这个并改变了

public class TreeViewCustomItem
{
    public string Header { get; set; }
}

和这个

 listmy = new ObservableCollection<TreeViewCustomItem> { new TreeViewCustomItem { Header = "xD" }, new TreeViewCustomItem { Header = "xxD" } };
        //treeView1.ItemsSource = listmy;
        this.DataContext = listmy;

 public class selector : DataTemplateSelector
{

    public DataTemplate RegulationTemplate { get; set; }

    public DataTemplate DefaultTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        TreeViewCustomItem treeViewItem = item as TreeViewCustomItem;
        if (treeViewItem.Header == "xD")
        {
            return RegulationTemplate;
        }

        else
        {
            return DefaultTemplate;

        }
    }
}

并且在XAML中看起来像这样

         xmlns:local="clr-namespace:WpfApplication1.Views">
    <Window.Resources>
        <DataTemplate x:Key="DefaultTemplate">
            <TextBlock Text="{Binding Header}"></TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="Regulation">
            <TextBlock Text="{Binding Path=Header}" FontWeight="Bold"></TextBlock>
        </DataTemplate>
    <local:selector x:Key="selector_" DefaultTemplate="{StaticResource DefaultTemplate}" RegulationTemplate="{StaticResource Regulation}"/>
</Window.Resources>
    <Grid>
        <TreeView Height="409" HorizontalAlignment="Left" Margin="10,10,0,0" Name="treeView1" VerticalAlignment="Top" Width="277"  
           ItemsSource="{Binding}" ItemTemplateSelector="{StaticResource selector_}"/>      

</Grid>

它起作用所以我的假设是问题在TreeViewCustomItem中。

答案 1 :(得分:0)

您的问题似乎是TreeViewCustomItem继承自TreeViewItem。 (见http://pastebin.com/jnP2nWMF

删除该继承(以及依赖项属性)会导致模板选择器触发。你想用节点项实现什么?

查看OutputWindow,我收到此消息:

System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='TreeViewCustomItem'

您不必将项目从TreeViewItem继承,以便将它们绑定到TreeViewTreeViewItemTreeView用于保存数据,然后DataTemplate用于显示数据。