Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Move DataTemplates from TreeView.Resources to Window.Resources </p> <pre><code> &lt;Window.Resources&gt;&lt;DataTemplate x:Key="DefaultTemplate"&gt; &lt;TextBlock Text="{Binding Header}"&gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt;&lt;DataTemplate x:Key="Regulation"&gt; &lt;TextBlock Text="{Binding Path=Header}" FontWeight="Bold"&gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="Article"&gt; &lt;TextBlock Text="{Binding Path=Header}" Foreground="Green"&gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;local:TreeViewItemTemplateSelector x:Key="TemplateSelector" DefaultTemplate="{StaticResource DefaultTemplate}" ArticleTemplate="{StaticResource Article}" RegulationTemplate="{StaticResource Regulation}" /&gt; </code></pre> <p>and make change</p> <pre><code> &lt;TreeView ItemTemplateSelector="{StaticResource TemplateSelector}" Height="409" HorizontalAlignment="Left" Margin="10,10,0,0" Name="treeView1" VerticalAlignment="Top" Width="277" ItemsSource="{Binding}"/&gt; </code></pre> <p>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</p> <pre><code>public class TreeViewCustomItem { public string Header { get; set; } } </code></pre> <p>and this</p> <pre><code> listmy = new ObservableCollection&lt;TreeViewCustomItem&gt; { 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; } } } </code></pre> <p>and in XAML looks like this</p> <pre><code> xmlns:local="clr-namespace:WpfApplication1.Views"&gt; &lt;Window.Resources&gt; &lt;DataTemplate x:Key="DefaultTemplate"&gt; &lt;TextBlock Text="{Binding Header}"&gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="Regulation"&gt; &lt;TextBlock Text="{Binding Path=Header}" FontWeight="Bold"&gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;local:selector x:Key="selector_" DefaultTemplate="{StaticResource DefaultTemplate}" RegulationTemplate="{StaticResource Regulation}"/&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;TreeView Height="409" HorizontalAlignment="Left" Margin="10,10,0,0" Name="treeView1" VerticalAlignment="Top" Width="277" ItemsSource="{Binding}" ItemTemplateSelector="{StaticResource selector_}"/&gt; &lt;/Grid&gt; </code></pre> <p>And it works so my presumption is that problem is inside TreeViewCustomItem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload