使用MVVM过滤WPF TreeView [英] Filter WPF TreeView using MVVM

查看:315
本文介绍了使用MVVM过滤WPF TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < TreeView ItemsSource ={Binding RootViewModels}
FontSize =12>
< TreeView.ItemContainerStyle>
< Style TargetType ={x:Type TreeViewItem}>
< Setter Property =local:TreeViewItemBehaviour.IsBroughtIntoViewWhenSelected
Value =True/>
< Setter Property =IsExpanded
Value ={Binding IsExpanded,Mode = TwoWay}/>
< Setter Property =IsSelected
Value ={Binding IsSelected,Mode = TwoWay}/>
< Setter Property =Visibility
Value ={Binding IsVisible,Mode = TwoWay,Converter = {StaticResource boolVisibilityConverter}}/>
< / style>
< TreeView.ItemTemplate>
< HierarchicalDataTemplate ItemsSource ={Binding Children}>
< TextBlock Text ={Binding SomeDisplayText}/>
< / HierarchicalDataTemplate>
< / TreeView>

其中 RootViewModels Children 类型为 ObservableCollection< SomeItemViewModel>



一个组合框,我希望这个组合框中的选定项作为过滤 TreeView 通过。我有一个ViewModelRepository也是类型 ObservableCollection< SomeItemViewModel> 并由我的IoC容器提供。我使用它来创建我在上面的XAML中的其他ObservableCollections。



目前,我正在设置 Visibility <当我选择了ComboBox中的一个项目时,我的ViewModelRepository中的每个SomeItemViewModel的code>。这似乎适用于树中前两个级别以下的任何内容,但不适用于第二级本身(第一级始终是可见的根级别)。



但是,它并不一致。当我应用过滤器时,正确的节点被设置为不可见,但是如果我然后展开包含已过滤节点的节点,那么随后的任何过滤器都将失败。

我已经阅读了有关将ItemsControls绑定到CollectionViewSource以进行过滤的功能,但是我无法找到与TreeView一起使用的示例。有没有人有任何指示我可以在这里使用它?

解决方案

您可以使用ICollectionView筛选您的集合。
这样的东西:

  ICollectionView view = CollectionViewSource.GetDefaultView(this.YourObservableCollection); 
view.Filter = o => {
var itemtofilter = o作为yourviewmodeltype;

//在这里检查你的条件
...

return(bool)itemtofilercondition;
};

编辑:如果您调用新的YourObservableCollection(),则必须重新创建视图。

I currently have a TreeView which has the following structure:

        <TreeView ItemsSource="{Binding RootViewModels}"
                  FontSize="12">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="local:TreeViewItemBehaviour.IsBroughtIntoViewWhenSelected"
                            Value="True" />
                    <Setter Property="IsExpanded"
                            Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected"
                            Value="{Binding IsSelected, Mode=TwoWay}" />
                    <Setter Property="Visibility"
                            Value="{Binding IsVisible, Mode=TwoWay, Converter={StaticResource boolVisibilityConverter}}" />
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding SomeDisplayText}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

where RootViewModels and Children are of type ObservableCollection<SomeItemViewModel>

In the same View I have a ComboBox and I want the selected item in this ComboBox to serve as the criteria to filter the TreeView by. I have a ViewModelRepository which is also of type ObservableCollection<SomeItemViewModel> and is provided by my IoC container. I use this to create my other ObservableCollections that are in the XAML above.

At the moment, I'm trying to set the Visibility of each SomeItemViewModel in my ViewModelRepository when an item in the ComboBox is selected. This seems to work for anything below the first two levels in the tree, but not for the 2nd level itself (the first level being the root which is always visible).

However, it doesn't work consistently. When I apply the "filter" the correct nodes are set invisible, but if I then expand a node which contains "filtered" nodes then any subsequent "filters" fail.

I've read about binding ItemsControls to a CollectionViewSource in order to do filtering, but I can't find an example of it's usage with the TreeView. Does anyone have any pointers on how I can use it here?

解决方案

you could use ICollectionView to filter your collection. something like this:

ICollectionView view = CollectionViewSource.GetDefaultView(this.YourObservableCollection);
view.Filter = o => {
                      var itemtofilter = o as yourviewmodeltype;

                      //check your conditions here
                      ...

                      return (bool)itemtofilercondition;
                    };

edit: you have to recreate the view if you call new YourObservableCollection();

这篇关于使用MVVM过滤WPF TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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