tags:

views:

1710

answers:

5

How would I clear the TreeView selection within a WPF TreeView? I have tried looping through the TreeNodes and clearing the IsSelected property however that is a ReadOnly property. Any ideas?

The TreeView is using XML Binding through the XMLDataProvider object.

+3  A: 

Not sure what you mean by TreeNodes.

Typically you would have a corresponding IsSelected property on your view model that your view binds to:

<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

Therefore, you would just loop through the data items in your view model and set IsSelected = false there.

However, it sounds like you don't have such a property. That being the case, you need to get the corresponding TreeViewItem for each data item. See the TreeView.ItemContainerGenerator property for info on how to do this. Something like:

var treeViewItem = _treeView.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
treeViewItem.IsSelected = false;

HTH, Kent

Kent Boogaart
A: 

Find the selected item and set the value:

private void Button_Click(object sender, RoutedEventArgs e)
{
  TreeViewItem tvi = treeviewExample.SelectedItem as TreeViewItem;
  if (tvi != null)
  {
    tvi.IsSelected = false;
  }
}
amaca
A: 

I came across the exact same problems and wrote the following code which will work on any treeview, with just a single line call to the first function.

class TomWrightsUtils
{
    public static void ClearTreeViewSelection(TreeView tv)
    {
        if (tv != null)
            ClearTreeViewItemsControlSelection(tv.Items, tv.ItemContainerGenerator);
    }
    private static void ClearTreeViewItemsControlSelection(ItemCollection ic, ItemContainerGenerator icg)
    {
        if ((ic != null) && (icg != null))
            for (int i = 0; i < ic.Count; i++)
            {
                TreeViewItem tvi = icg.ContainerFromIndex(i) as TreeViewItem;
                if (tvi != null)
                {
                    ClearTreeViewItemsControlSelection(tvi.Items, tvi.ItemContainerGenerator);
                    tvi.IsSelected = false;
                }
            }
    }
}
Tom Wright
A: 

This seems to work so far, but i just put it in like 5 minutes ago so use at your own risk. I basically wanted to clear the selection when user clicks within the tree control, but not on a tree item.

 void DestinationTree_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
         TreeView tree = sender as TreeView;

         DestinationClientViewModel selectedItem = tree.SelectedItem as DestinationClientViewModel;

         if (selectedItem != null)
         {
            int selectedItemIndex = this.DestinationTree.Items.IndexOf(selectedItem);

            if (selectedItemIndex > -1)
            {
               TreeViewItem tvi = this.DestinationTree.ItemContainerGenerator.ContainerFromIndex(selectedItemIndex) as TreeViewItem;

               if (tvi != null)
                  tvi.IsSelected = false;
            }
         }


      }
Ben Dempsey
Ignore my code. Only works at the root level. Any child nodes selected will not get cleared.
Ben Dempsey
+1  A: 

This works out great as a extention method so you can call

youTreeview.ClearSelection();

using System.Windows.Forms;
using System.Windows.Controls;

namespace YourAppNamespace
{
    public static void ClearSelection(this TreeView input)
    {
    // this should be some container that you put in
    // possibly the actual treeviewitem, not sure on that though
    var selected = input.SelectedItem;
    if (selected == null)
        return;

    // in my case this works perfectly
    var tvi = input.ItemContainerGenerator.ContainerFromItem(selected) as TreeViewItem;
    var tvi = input.ItemContainerGenerator.ContainerFromItem(selected) as TreeViewItem;
    if (tvi == null)
    {
        // it must be a child, heres a hack fix
        // my nodes are inherited from TreeViewItemViewModel by Josh Smith
        var child = selected as WPF.Controls.TreeViewItemViewModel;
        if (child == null)
            return;
        child.IsSelected = false;

    }
    else
        tvi.IsSelected = false;
     }


}
Gauthier