Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically select an item in a WPF TreeView?

How is it possible to programmatically select an item in a WPF TreeView? The ItemsControl model seems to prevent it.

like image 641
Thomas Bratt Avatar asked Jan 05 '09 17:01

Thomas Bratt


People also ask

How do I know if TreeView node is selected?

Solution 1Use TreeView. SelectedNode[^] property, which get or set selected node for currently selected Treeview. If no TreeNode is currently selected, the SelectedNode property is a null reference (Nothing in Visual Basic).

What is TreeView item?

TreeViewItem is a HeaderedItemsControl, which means its header and collection of objects can be of any type (such as string, image, or panel). For more information, see the HeaderedItemsControl class.

What is TreeView in WPF?

The TreeView control provides a way to display information in a hierarchical structure by using collapsible nodes. This topic introduces the TreeView and TreeViewItem controls, and provides simple examples of their use.


2 Answers

For those who are still looking for the right solution to this problem here is the one below. I found this one in the comments to the Code Project article “WPF TreeView Selection” http://www.codeproject.com/KB/WPF/TreeView_SelectionWPF.aspx by DaWanderer. It was posted by Kenrae on Nov 25 2008. This worked great for me. Thanks Kenrae!

Here is his post:

Instead of walking the tree, have your own data object have the IsSelected property (and I recommend the IsExpanded property too). Define a style for the TreeViewItems of the tree using the ItemContainerStyle property on the TreeView that binds those properties from the TreeViewItem to your data objects. Something like this:

<Style x:Key="LibraryTreeViewItemStyle"                TargetType="{x:Type TreeViewItem}">             <Setter Property="IsExpanded"                         Value="{Binding IsExpanded, Mode=TwoWay}" />             <Setter Property="IsSelected"                         Value="{Binding IsSelected, Mode=TwoWay}" />             <Setter Property="FontWeight"                         Value="Normal" />             <Style.Triggers>                   <Trigger Property="IsSelected"                               Value="True">                         <Setter Property="FontWeight"                                     Value="Bold" />                   </Trigger>             </Style.Triggers>       </Style>  <TreeView ItemsSource="{Binding Path=YourCollection}"                ItemContainerStyle="{StaticResource LibraryTreeViewItemStyle}"                ItemTemplate={StaticResource YourHierarchicalDataTemplate}/> 
like image 174
kuninl Avatar answered Sep 18 '22 15:09

kuninl


It's a real pain for some strange reason, you have to use ContainerFromItem to get the container, then invoke the select method.

//  selectedItemObject is not a TreeViewItem, but an item from the collection that  //  populated the TreeView.  var tvi = treeView.ItemContainerGenerator.ContainerFromItem(selectedItemObject)            as TreeViewItem;  if (tvi != null) {     tvi.IsSelected = true; } 

There once was a blog entry on how to do it here, but the link is dead now.

like image 37
Steven Robbins Avatar answered Sep 18 '22 15:09

Steven Robbins