过滤TreeView而不折叠其节点

过滤TreeView而不折叠其节点

本文介绍了WPF:过滤TreeView而不折叠其节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题.>

我过滤 TreeView 控件的顶级节点,如下所示.

  private void ApplyFilterHandler(对象发送者,RoutedEventArgs e){如果(_filterCheckBox.IsChecked.Value)CollectionViewSource.GetDefaultView(TopLevelNodes).Filter + = MyFilter;别的CollectionViewSource.GetDefaultView(TopLevelNodes).Filter-= MyFilter;} 

.

 < TreeView ItemsSource ="{Binding TopLevelNodes}">...</TreeView> 

当用户应用过滤器时,所有节点都会折叠.

问题
如何在保留其他节点的展开状态的同时隐藏树中的某些节点?
有人可以解释一下, ICollectionView.Filter + = MyFilter 内部发生了什么.

感谢您的时间.

解决方案

在这种情况下 Reflector 永远是您的朋友.我假设在内部调用 ICollectionView.Refresh()以反映由于添加/删除过滤器而引起的更改.这样可以有效地重新枚举 TreeView 中的节点.

要对此进行补偿,您始终可以获取所有树项的状态,并在刷新后重新应用它们.这可能不是您想要的简单解决方案.

您可能还想尝试一次设置 MyFilter 并以编程方式调用 ICollectionView.Refresh().然后,您的 MyFilter 应该根据 _filterCheckBox.IsChecked 值进行过滤.这可能会有所作为,但这些全都只是想法.您必须自己尝试.

This one is a follow-up question.

I filter the top level nodes of a TreeView control like shown below.

private void ApplyFilterHandler(object sender, RoutedEventArgs e)
{
    if (_filterCheckBox.IsChecked.Value)
        CollectionViewSource.GetDefaultView(TopLevelNodes).Filter += MyFilter;
    else
        CollectionViewSource.GetDefaultView(TopLevelNodes).Filter -= MyFilter;
}

.

<TreeView ItemsSource="{Binding TopLevelNodes}">
  ...
</TreeView>

When the user applies the filter all nodes get collapsed.

Question
How can I hide certain nodes in a tree while retaining the expand state of the other nodes?
Can someone explain, what happens internally on ICollectionView.Filter += MyFilter.

Thanks for your time.

解决方案

In cases such as this Reflector is always your friend. I presume that ICollectionView.Refresh() is called internally to reflect the changes caused by adding/removing a filter. This effectively re-enumerates the nodes in the TreeView.

To compensate for this you can always grab the state of all tree items and re-apply them after a refresh. This may not be the easy solution you were looking for.

You may also want to try to set MyFilter once and call ICollectionView.Refresh() programmatically. Your MyFilter should then filter according to _filterCheckBox.IsChecked value. This could make a difference but these are all merely ideas. You must try them yourself.

这篇关于WPF:过滤TreeView而不折叠其节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:13