IN WPF, HOW DO YOU PROGRAMMATICALLY DESELECT ALL ITEMS IN A TREEVIEW?

Total Post:70

Points:494
WPF 
WPF
 5170  View(s)
Ratings:
Rate this:

I'm trying to create a recursive method to deselect all items in a WPF TreeView. The thing that complicates things is that each TreeViewItem is not a mini-TreeView. This causes you to have to do a lot of casting back and forth. So, here's what I have tried:

TreeViewDeselectAll(myTreeView.Items);     
// Must send in ItemCollection to allow the recursive call
private void TreeViewDeselectAll(ItemCollection myTreeViewItems)
{
    // The only way to get to the IsSelected property is to turn it back into a TreeViewItem
    foreach (TreeViewItem currentItem in myTreeViewItems)
    {
        currentItem.IsSelected = false;
        if (currentItem.HasItems)
        {
             // Recursify!
             TreeViewDeselectAll(currentItem.Items);
        }
    }
}

Has anyone successfully deselected all items in a TreeView? Have you even been able to traverse a TreeView in a recursive manner?

The Winforms TreeView has a Nodes collection, which is really a mini-TreeView. This allows recursion just fine. But the WPF TreeView does not have Nodes.

Working in .Net 4.0.

  1. Post:412

    Points:2888
    Re: In WPF, how do you programmatically deselect all Items in a Treeview?

    Hi Lois,


    Each TreeViewItem is a Mini-TreeView. You got the wrong impression or you read something wrong about TreeViewItems.

    What you doing wrong is passing the Items collection which doesnt contain children of type TreeViewItem but instead those are the data items.

    Pass the children collection and you should do fine.

    Like this:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private bool @switch = false;
        private void TreeViewDeselectAll(IEnumerable myTreeViewItems, bool value)
        {
            if (myTreeViewItems != null)
            {
                foreach (var currentItem in myTreeViewItems)
                {
                    if (currentItem is TreeViewItem)
                    {
                        TreeViewItem item = (TreeViewItem)currentItem;
                        item.IsSelected = value;
                        if (item.HasItems)
                        {
                            TreeViewDeselectAll(LogicalTreeHelper.GetChildren(item), value);
                        }
                    }
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.TreeViewDeselectAll(LogicalTreeHelper.GetChildren(this.treeView), this.@switch);
        }
    }

    XAML would look like this for example:

    <StackPanel>
        <TreeView Name="treeView">
            <TreeViewItem Header="First Level">
                <TreeViewItem Header="Second Level"/>
            </TreeViewItem>
        </TreeView>
        <Button Click="Button_Click">select/unselect all</Button>
    </StackPanel>

    I changed your TreeViewDeselectAll method a little bit. Based on switch you can select or unselect all.

      Modified On Apr-04-2018 01:01:01 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!