views:

212

answers:

1

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.

A: 

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.

wpfwannabe
"You may also want to try to set MyFilter once and call `ICollectionView.Refresh()` programmatically."Tried it. It makes no difference. I went with your first suggestion and took the long road. ;)
Julian Lettner
Could you provide an example of how you use the Reflector to get the tree's state?
luke