---
title: "In WPF, how do you programmatically deselect all Items in a Treeview?"  
description: "In WPF, how do you programmatically deselect all Items in a Treeview?"  
author: "Anonymous User"  
published: 2013-12-23  
updated: 2013-12-23  
canonical: https://www.mindstick.com/forum/1837/in-wpf-how-do-you-programmatically-deselect-all-items-in-a-treeview  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# In WPF, how do you programmatically deselect all Items in a Treeview?

I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to create a recursive [method](https://www.mindstick.com/forum/166/webservice-method) to deselect all [items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios) in a [WPF](https://www.mindstick.com/forum/304/wpf) [TreeView](https://www.mindstick.com/forum/1180/how-to-create-a-treeview-from-xml-file-in-c-sharp). The thing that complicates things is that each TreeViewItem is not a mini-TreeView. This [causes](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources) 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 callprivate 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](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-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](https://www.mindstick.com/news/2242/on-thursday-google-cloud-announced-the-debut-of-its-own-node-hosting-service-for-web3-developers) [collection](https://www.mindstick.com/articles/1718/collections-in-java), which is really a mini-TreeView. This allows [recursion](https://answers.mindstick.com/qa/111681/what-is-recursion-and-when-should-i-use-it) just fine. But the WPF TreeView does not have Nodes.

Working in .Net 4.0.

## Replies

### Reply by Pravesh Singh

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.


---

Original Source: https://www.mindstick.com/forum/1837/in-wpf-how-do-you-programmatically-deselect-all-items-in-a-treeview

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
