forum

Home / DeveloperSection / Forums / In WPF, how do you programmatically deselect all Items in a Treeview?

In WPF, how do you programmatically deselect all Items in a Treeview?

Anonymous User 7232 23-Dec-2013

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.


wpf wpf 
Updated on 23-Dec-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By