在WPF树视图中分组数据 [英] Grouping data in WPF treeview

查看:378
本文介绍了在WPF树视图中分组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两个分组选项(单选按钮)创建一个WPF TreeView 。因此,数据将在两级层次结构中以两种不同的方式进行分组,最低级别是实际的数据项,而这些组只是一种表示数据以便于理解的方法。

I want to create a WPF TreeView with two grouping options (radio buttons). So the data will be grouped in two different ways in a 2-level hierarchy, the lowest level being the actual data items, and the groups being just a way to represent the data for easier understanding.

他们也可以按组(复选框)选择项目,但我已经弄清楚了这一点,例如如果我想表示数据库对象,并希望通过模式或对象类型(表,视图,函数等)将它们分组。

They would also be able to select items by group (checkboxes) but I got that part already figured out, e.g. if I want to represent database objects and want to have them grouped either by schema or by object type (table, view, function, etc.).

不知道我应该从两种分组模式开始。每当分组模式发生变化或有更直接的方式时,我应该完全重组我的 ObservableCollection

I just don't know how I should start on the two grouping modes. Should I entirely restructure my ObservableCollection whenever the grouping mode changes or is there a more straightforward way?

另外,什么如果第二级的 DataTemplate 根据分组模式略有不同,例如按组对象类型分组时,您需要在级别2上显示模式?

Also, what if my DataTemplate for the 2nd level would be slightly different depending on the grouping mode, for example when grouped by object type you need to display the schema on level 2?

任何人都可以给我一些关于如何启动和使用哪些技术的提示?

Can anyone give me some tips on how to start and which techniques to use?

推荐答案

通过设置 GroupDescriptions 在它的CollectionViewSource。您可以通过执行以下操作来执行代码:

Group your collection by setting GroupDescriptions on its CollectionViewSource. You can do it in code by doing something like this:

CollectionViewSource.GetDefaultView(yourCollection).GroupDescriptions.Add(
    new PropertyGroupDescription("PropertyName"));

或者您可以通过创建 CollectionViewSource

Or you can do it in XAML by creating a CollectionViewSource explicitly.

    <CollectionViewSource
        Source="{StaticResource yourCollection}"
        xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework">
        <CollectionViewSource.GroupDescriptions>
            <dat:PropertyGroupDescription PropertyName="PropertyName"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

如果您使用像ListBox这样的简单的ItemsControl,那么您只需设置 GroupStyle 属性。如果你想使用TreeView,那么我想你想绑定到属性。你应该阅读Bea Stollnitz的博客文章:

If you are using a plain ItemsControl like ListBox then you can just set the GroupStyle property. If you want to use a TreeView, then I think you want to bind to the Groups property on the ICollectionView. You should read Bea Stollnitz's blog entries on grouping:

  • How do I display grouped data in a TreeView?
  • How can I do custom Grouping?

这篇关于在WPF树视图中分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆