Search code examples
c#.netwpfdatatemplateselector

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:


Solution

  • 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.