简体   繁体   中英

WPF datatemplateselector not getting called

Hello I'm trying to dynamically change datatemplate but my method SelectTemplate in class TreeViewItemTemplateSelector never getting called (I've checked it by debugger) :( please help me :)

Code from xaml MainWindow:

Code in code behind:

Move DataTemplates from TreeView.Resources to 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}"  />

and make change

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

Update code and we will see. I put similar code into VS and it works so we need to take a closer look. So i checked this and changed

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

and this

 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;

        }
    }
}

and in XAML looks like this

         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>

And it works so my presumption is that problem is inside TreeViewCustomItem.

Your problem seems to be that your TreeViewCustomItem is inheriting from TreeViewItem . (As seen in http://pastebin.com/jnP2nWMF )

Removing that inheritance (and the dependency property) causes the template selector to fire fine. What were/are you trying to achieve with the node item?

Looking at the OutputWindow, I get this message:

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

You don't have to have items inherit from the TreeViewItem in order to bind them to a TreeView , TreeViewItem is something that the TreeView uses to hold the data, and then the DataTemplate is used to present the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM