WPF TreeView 动态分组

发布于 2024-10-10 09:46:53 字数 7385 浏览 3 评论 0原文

我正在尝试在 WPF 中创建一个绑定到 CollectionViewSource 的 TreeView。我在 CollectionViewSource 中创建组,并在 XAML 中设置 HierarchicalDataTemplate 以正确显示 TreeView。

在我的 ViewModel 中,我有一种方法可以更改 CollectionViewSource 的分组,并且一切似乎都运行良好。我遇到的唯一问题是在没有任何分组的情况下显示 CollectionViewSource。

有谁知道如何设计模板以适应 CollectionViewSource 没有分组的场景,但也可以容纳带有分组的 CollectionViewSource ?

更新 我创建了一些示例代码来更好地描述我正在做的事情。 DataTemplateSelector 在应用程序启动时工作,但我不知道当用户从组合框中选择不同的分组选项时如何重新触发 DataTemplate 选择器。下面是我的示例代码

<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">

<Window.Resources>

    <local:SchoolTemplateSelector x:Key="schoolTemplateSelector" />

    <HierarchicalDataTemplate x:Key="BasicList" ItemsSource="{Binding TeachersBy.Source}">
        <StackPanel>
            <TextBlock Text="{Binding Name}" />
            <ComboBox SelectionChanged="ComboBox_SelectionChanged" ItemsSource="{Binding GroupByList}" />
        </StackPanel>
        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Last}" />
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="GroupList" ItemsSource="{Binding TeachersBy.View.Groups}">
        <StackPanel>
            <TextBlock Text="{Binding Name}" />
            <ComboBox SelectionChanged="ComboBox_SelectionChanged" ItemsSource="{Binding GroupByList}" />  
        </StackPanel>
        <HierarchicalDataTemplate.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Items}">
                <TextBlock Text="{Binding Name}" />
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Last}" />
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</Window.Resources>

<StackPanel>        
    <TreeView  ItemsSource="{Binding Schools}" ItemTemplateSelector="{Binding schoolTemplateSelector}" />
</StackPanel>

和背后的代码

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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollection<School> 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<School> {
            new School
            {
                Name = "Apple",
                Teachers = new ObservableCollection<Teacher> {
                    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<Teacher> {
                    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<Teacher> Teachers { get; set; }

    public CollectionViewSource TeachersBy { get; set; }

    public ObservableCollection<String> GroupByList { get; set; }

    public School()
    {
        Teachers = new ObservableCollection<Teacher>();
        TeachersBy = new CollectionViewSource();

        GroupByList = new ObservableCollection<string> {
            "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 && (item as School).TeachersBy.GroupDescriptions.Count > 0)
        {
            return
                element.FindResource("GroupList")
                       as DataTemplate;
        }
        else
        {
            return
                element.FindResource("BasicList")
                       as DataTemplate;
        }
    }
}
}

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.

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.

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?

Update
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

<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">

<Window.Resources>

    <local:SchoolTemplateSelector x:Key="schoolTemplateSelector" />

    <HierarchicalDataTemplate x:Key="BasicList" ItemsSource="{Binding TeachersBy.Source}">
        <StackPanel>
            <TextBlock Text="{Binding Name}" />
            <ComboBox SelectionChanged="ComboBox_SelectionChanged" ItemsSource="{Binding GroupByList}" />
        </StackPanel>
        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Last}" />
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="GroupList" ItemsSource="{Binding TeachersBy.View.Groups}">
        <StackPanel>
            <TextBlock Text="{Binding Name}" />
            <ComboBox SelectionChanged="ComboBox_SelectionChanged" ItemsSource="{Binding GroupByList}" />  
        </StackPanel>
        <HierarchicalDataTemplate.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Items}">
                <TextBlock Text="{Binding Name}" />
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Last}" />
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</Window.Resources>

<StackPanel>        
    <TreeView  ItemsSource="{Binding Schools}" ItemTemplateSelector="{Binding schoolTemplateSelector}" />
</StackPanel>

and the code behind

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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollection<School> 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<School> {
            new School
            {
                Name = "Apple",
                Teachers = new ObservableCollection<Teacher> {
                    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<Teacher> {
                    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<Teacher> Teachers { get; set; }

    public CollectionViewSource TeachersBy { get; set; }

    public ObservableCollection<String> GroupByList { get; set; }

    public School()
    {
        Teachers = new ObservableCollection<Teacher>();
        TeachersBy = new CollectionViewSource();

        GroupByList = new ObservableCollection<string> {
            "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 && (item as School).TeachersBy.GroupDescriptions.Count > 0)
        {
            return
                element.FindResource("GroupList")
                       as DataTemplate;
        }
        else
        {
            return
                element.FindResource("BasicList")
                       as DataTemplate;
        }
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2024-10-17 09:46:53

您可以通过创建绑定到间隔变量的转换器来重新触发 ItemTemplateSelector。

You can refire the ItemTemplateSelector by creating a Converter thats bound to an interval variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文