Note that there are some explanatory texts on larger screens.

plurals
  1. POChange TreeViewItem Template when IsSelected and two types using in TreeView
    text
    copied!<p>In my TreeView I use two differnt classes for binding. For example, I have a Group what can have ChildGroup and can have Items. Example code of this classes:</p> <pre><code>using System.Collections.Generic; using System.Collections.ObjectModel; namespace WpfApplication1 { public class Group { public Group(string name) { Name = name; items = new ObservableCollection&lt;Item&gt;(); groups = new ObservableCollection&lt;Group&gt;(); } public string Name { get; set; } private ObservableCollection&lt;Item&gt; items; private ObservableCollection&lt;Group&gt; groups; public ObservableCollection&lt;Item&gt; Items { get { return items; } } public ObservableCollection&lt;Group&gt; Groups { get { return groups; } } public IEnumerable&lt;object&gt; AllItems { get { foreach (var group in groups) { yield return group; } foreach (var item in items) { yield return item; } } } } public class Item { public Item(string name) { ItemName = name; } public string ItemName { get; set; } } } </code></pre> <p>To bind it to TreeView I use following template</p> <pre><code>&lt;Grid&gt; &lt;TreeView Name="treeView"&gt; &lt;TreeView.Resources&gt; &lt;HierarchicalDataTemplate DataType="{x:Type WpfApplication1:Group}" ItemsSource="{Binding AllItems}"&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;/HierarchicalDataTemplate&gt; &lt;DataTemplate DataType="{x:Type WpfApplication1:Item}"&gt; &lt;TextBlock Text="{Binding ItemName}" FontStyle="Italic"/&gt; &lt;/DataTemplate&gt; &lt;/TreeView.Resources&gt; &lt;/TreeView&gt; &lt;/Grid&gt; </code></pre> <p>It is easy.</p> <p>The problem is that I need to change ItemTemplate when Is selected. And I need to change only then Item class selected.</p> <p>I can do it if only one class use for binding. It also easy using Style and Trigger, like this:</p> <pre><code>&lt;TreeView Name="treeView1" Grid.Column="1"&gt; &lt;TreeView.Resources&gt; &lt;HierarchicalDataTemplate DataType="{x:Type WpfApplication1:Group}" ItemsSource="{Binding AllItems}" x:Key="groupTemplate"&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType="{x:Type WpfApplication1:Group}" ItemsSource="{Binding AllItems}" x:Key="selectedGroupTemplate"&gt; &lt;TextBlock Text="{Binding Name}" FontStyle="Italic" FontWeight="Bold" FontSize="14"/&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.Resources&gt; &lt;TreeView.ItemContainerStyle&gt; &lt;Style TargetType="{x:Type TreeViewItem}"&gt; &lt;Setter Property="HeaderTemplate" Value="{StaticResource groupTemplate}"/&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter Property="HeaderTemplate" Value="{StaticResource selectedGroupTemplate}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/TreeView.ItemContainerStyle&gt; &lt;/TreeView&gt; </code></pre> <p>But I have a trouble for multiclass binding.</p> <p>How can I change SelectedItem template then multiclass binding using? Any ideas?</p> <p>My code behind sample:</p> <pre><code>using System.Collections.ObjectModel; using System.Windows; namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for Window2.xaml /// &lt;/summary&gt; public partial class Window2 : Window { private ObservableCollection&lt;Group&gt; _groups; public ObservableCollection&lt;Group&gt; Groups { get { return _groups; } } public Window2() { InitializeComponent(); InitGroups(); treeView.ItemsSource = _groups; treeView1.ItemsSource = _groups; } private void InitGroups() { _groups = new ObservableCollection&lt;Group&gt;(); Group group1 = new Group("Group1"); group1.Groups.Add(new Group("Group1.1")); group1.Groups.Add(new Group("Group1.2")); group1.Groups.Add(new Group("Group1.3")); group1.Items.Add(new Item("Item1.1")); group1.Items.Add(new Item("Item1.2")); group1.Groups[1].Items.Add(new Item("Item1.2.1")); group1.Groups[1].Items.Add(new Item("Item1.2.2")); _groups.Add(group1); Group group2 = new Group("Group2"); group2.Groups.Add(new Group("Group2.1")); group2.Groups.Add(new Group("Group2.2")); group2.Items.Add(new Item("Item2.1")); group2.Groups[0].Items.Add(new Item("Item2.1.1")); group2.Groups[0].Items.Add(new Item("Item2.1.1")); _groups.Add(group2); } } } </code></pre> <p>Result <img src="https://i.stack.imgur.com/pEjY9.png" alt="Result"></p> <p>Now I think to use TreeView.HeaderTemplateSelector, but may be exists way to use only xaml.</p> <p>Thanks.</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