Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF TreeView Dynamic Grouping
    primarykey
    data
    text
    <p>I'm attempting to create a TreeView in WPF that is bound to a CollectionViewSource. I create groups in the CollectionViewSource and have the HierarchicalDataTemplate setup in the XAML to display the TreeView properly.</p> <p>In my ViewModel I have a method to change the Grouping of the CollectionViewSource and all seems to work well. The only issue I have is displaying the CollectionViewSource without any grouping.</p> <p>Does anyone know how to design the template to accommodate a scenario where the CollectionViewSource has no groupings, but can also accommodate a CollectionViewSource with groupings?</p> <p><strong>Update</strong> I have created some sample code to better describe what I'm doing. The DataTemplateSelector works when the app starts but I can't figure out how to re-fire the DataTemplate Selector when the user selects a different grouping option from the combobox. Below is my sample code</p> <pre><code>&lt;Window x:Class="TreeViewGroupTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TreeViewGroupTest" Title="WindowsApplication1" Height="Auto" Width="300"&gt; &lt;Window.Resources&gt; &lt;local:SchoolTemplateSelector x:Key="schoolTemplateSelector" /&gt; &lt;HierarchicalDataTemplate x:Key="BasicList" ItemsSource="{Binding TeachersBy.Source}"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;ComboBox SelectionChanged="ComboBox_SelectionChanged" ItemsSource="{Binding GroupByList}" /&gt; &lt;/StackPanel&gt; &lt;HierarchicalDataTemplate.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Last}" /&gt; &lt;/DataTemplate&gt; &lt;/HierarchicalDataTemplate.ItemTemplate&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate x:Key="GroupList" ItemsSource="{Binding TeachersBy.View.Groups}"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;ComboBox SelectionChanged="ComboBox_SelectionChanged" ItemsSource="{Binding GroupByList}" /&gt; &lt;/StackPanel&gt; &lt;HierarchicalDataTemplate.ItemTemplate&gt; &lt;HierarchicalDataTemplate ItemsSource="{Binding Items}"&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;HierarchicalDataTemplate.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Last}" /&gt; &lt;/DataTemplate&gt; &lt;/HierarchicalDataTemplate.ItemTemplate&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/HierarchicalDataTemplate.ItemTemplate&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;TreeView ItemsSource="{Binding Schools}" ItemTemplateSelector="{Binding schoolTemplateSelector}" /&gt; &lt;/StackPanel&gt; </code></pre> <p></p> <p>and the code behind</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections.ObjectModel; namespace TreeViewGroupTest { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public ObservableCollection&lt;School&gt; Schools { get; set; } public SchoolTemplateSelector schoolTemplateSelector { get; set; } private string group = "Subject"; public string GroupByChoice { get; set; } public MainWindow() { InitializeComponent(); GroupByChoice = "Subject"; Schools = new ObservableCollection&lt;School&gt; { new School { Name = "Apple", Teachers = new ObservableCollection&lt;Teacher&gt; { new Teacher { Last = "Alpha", Subject = "Math" , Grade = "9th" }, new Teacher { Last = "Beta", Subject = "English" , Grade = "9th" }, new Teacher { Last = "Charlie", Subject = "Math" , Grade = "9th" }, new Teacher { Last = "Delta", Subject = "English" , Grade = "10th" }, new Teacher { Last = "Echo", Subject = "Math" , Grade = "10th" }, new Teacher { Last = "Foxtrot", Subject = "English" , Grade = "10th" }, } }, new School { Name = "Microsoft", Teachers = new ObservableCollection&lt;Teacher&gt; { new Teacher { Last = "Alpha", Subject = "Math" , Grade = "9th" }, new Teacher { Last = "Beta", Subject = "English" , Grade = "9th" }, new Teacher { Last = "Charlie", Subject = "Math" , Grade = "9th" }, new Teacher { Last = "Delta", Subject = "English" , Grade = "10th" }, new Teacher { Last = "Echo", Subject = "Math" , Grade = "10th" }, new Teacher { Last = "Foxtrot", Subject = "English" , Grade = "10th" }, } }, }; Schools[0].SetTeacher(); ; Schools[1].GroupBy("Subject"); Schools[1].TeachersBy.View.Refresh(); this.DataContext = this; schoolTemplateSelector = new SchoolTemplateSelector(); } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string prop = e.AddedItems[0].ToString(); if (prop != "None") { foreach (School s in Schools) { s.GroupBy(prop); } } else { foreach (School s in Schools) { s.TeachersBy.GroupDescriptions.Clear(); } } //The DataTemplateSelector should fire now... } } public class School { public string Name { get; set; } public ObservableCollection&lt;Teacher&gt; Teachers { get; set; } public CollectionViewSource TeachersBy { get; set; } public ObservableCollection&lt;String&gt; GroupByList { get; set; } public School() { Teachers = new ObservableCollection&lt;Teacher&gt;(); TeachersBy = new CollectionViewSource(); GroupByList = new ObservableCollection&lt;string&gt; { "None", "Subject", "Grade" }; } public void SetTeacher() { TeachersBy.Source = Teachers; } public void GroupBy(string propertyName) { TeachersBy.Source = Teachers; TeachersBy.GroupDescriptions.Clear(); TeachersBy.GroupDescriptions.Add(new PropertyGroupDescription(propertyName)); TeachersBy.View.Refresh(); } } public class Teacher { public string Last { get; set; } public string Subject { get; set; } public string Grade { get; set; } public Teacher() { } } public class SchoolTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { FrameworkElement element = container as FrameworkElement; if (item is School &amp;&amp; (item as School).TeachersBy.GroupDescriptions.Count &gt; 0) { return element.FindResource("GroupList") as DataTemplate; } else { return element.FindResource("BasicList") as DataTemplate; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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