There is a DataTemplateSelector :
class DisplayTemplateSelector : DataTemplateSelector { public DataTemplate TemplateA { get; set; } public DataTemplate TemplateB { get; set; } public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) { var baseClass = item as BaseClass; switch (baseClass.Type) { case A: return TemplateA; case B: return TemplateB; default: return null; } } } XAML
<HierarchicalDataTemplate x:Key="key" ItemsSource="{Binding Streams, Mode=OneWay}" ItemTemplateSelector="{StaticResource displayTemplateSelector}"> <TextBlock Text="{Binding Name}"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type A}" x:Key="a"> <TextBlock Text="{Binding }"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type B}" x:Key="b"> <TextBlock Text="{Binding }"/> </HierarchicalDataTemplate> At this stage, everything works. But as soon as I want to set the ItemSource and ItemTemplate for one of the A or B templates, I get an exception:
Duplex assembly requires Path or XPath.
and a warning in the output:
Both 'ItemTemplate' and 'ItemTemplateSelector' are set; 'ItemTemplateSelector' will be ignored. TreeViewItem:'TreeViewItem' (Name='')
What is the problem?
UDP:
The problem was due to the binding in templates A, B.
<TextBlock Text="{Binding }"/> it was necessary to be attached to a specific field.