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

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

问题描述

我想创建一个WPF TreeView控件有两个分组选项(单选按钮)。这样的数据将在两种不同的方式中的2级层级,最低水平为实际的数据项,并且这些组被正好表示为了容易理解的数据的方式进行分组。

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 明确。

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

如果您使用的是普通的ItemsControl像列表框,那么你可以只设置的 GroupStyle 财产。如果你想使用一个TreeView,那么我认为你要绑定到的上的ICollectionView的财产。你应该阅读的分组衣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天全站免登陆