如何筛选使用的ICollectionView一个WPF树状层次结构? [英] How to filter a wpf treeview hierarchy using an ICollectionView?

查看:615
本文介绍了如何筛选使用的ICollectionView一个WPF树状层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含此数据的假设树视图:

I have a hypothetical tree view that contains this data:

RootNode
   Leaf
   vein
SecondRoot
   seeds
   flowers

我想筛选节点为了显示仅包含特定文本的节点。说,如果我指定的L,树会被过滤,仅显示RootNode->叶和SecondRoot->花(因为它们都包含字母L)。

I am trying to filter the nodes in order to show only the nodes that contain a certain text. Say if I specify "L", the tree will be filtered and show only RootNode->Leaf and SecondRoot->flowers (because they both contain the letter L).

继MV-VM模式,我有一个基本的TreeViewViewModel类是这样的:

Following the m-v-vm pattern, I have a basic TreeViewViewModel class like this:

public class ToolboxViewModel
{
    ...
    readonly ObservableCollection<TreeViewItemViewModel> _treeViewItems = new ObservableCollection<TreeViewItemViewModel>();
    public ObservableCollection<TreeViewItemViewModel> Headers
    {
        get { return _treeViewItems; }
    }

    private string _filterText;
    public string FilterText
    {
        get { return _filterText; }
        set
        {
            if (value == _filterText)
                return;

            _filterText = value;

            ICollectionView view = CollectionViewSource.GetDefaultView(Headers);
            view.Filter = obj => ((TreeViewItemViewModel)obj).ShowNode(_filterText);
        }
    }
    ...
}

而一个基本的TreeViewItemViewModel:

And a basic TreeViewItemViewModel:

public class ToolboxItemViewModel
{
    ...
    public string Name { get; private set; }
    public ObservableCollection<TreeViewItemViewModel> Children { get; private set; }
    public bool ShowNode(string filterText)
    {
        ... return true if filterText is contained in Name or has children that contain filterText ... 
    } 
    ...
}

一切都设置在XAML,所以我看到TreeView和搜索框。

Everything is setup in the xaml so I see the treeview and search box.

在此代码行使,过滤仅适用于这是不够的根节点。有没有一种方法,使过滤器涓滴在节点层次,使我的谓词呼吁每个节点?换句话说,可以过滤器被应用到TreeView作为一个整体?

When this code is exercised, the filter only applies to the Root nodes which is insufficient. Is there a way to make the filter trickle down in the hierarchy of nodes so that my predicate is called for every node ? In other words, can the filter be applied to the TreeView as a whole ?

推荐答案

不幸的是,没有办法让同一个过滤器适用于自动所有节点。过滤器是ItemsCollection的属性(不是DP)这是不是DependencyObject的,因此DP值继承是不存在的。

Unfortunately there is no way to make same Filter apply to all nodes automatically. Filter is a property (not a DP) of ItemsCollection which is not DependencyObject and so DP Value inheritance isn't there.

在树中的每个节点都有自己的ItemsCollection这有自己的过滤器。 。让它工作的唯一方法是进行手动设置的所有调用相同的委托

Each node in the tree has its own ItemsCollection which has its own Filter. The only way to make it work is to manually set them all to call the same delegate.

最简单的方法是将暴露类型谓词<的Filter属性;对象>你ToolBoxViewModel并在其二传手触发事件。然后ToolboxItemViewModel将负责消耗此事件,并更新其过滤器。

Simplest way would be to expose Filter property of type Predicate<object> at your ToolBoxViewModel and in its setter fire an event. Then ToolboxItemViewModel will be responsible for consuming this event and updating its Filter.

漂亮是不是没有,我不知道的性能会是什么样在大量项目树。

Aint pretty and I'm not sure what the performance would be like for large amounts of items in the tree.

这篇关于如何筛选使用的ICollectionView一个WPF树状层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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