tags:

views:

1455

answers:

4

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

+5  A: 

It's a pain in the ass for some strange reason, you have to use ContainerFromItem to get the container, then invoke the select method. There's a blog entry on how to do it here.

Steven Robbins
+2  A: 

You need to get the TreeViewItem and then set IsSelected to true.

HTH, Kent

Kent Boogaart
A: 

This is not as simple as it looks, the link provided by Steven has a solution posted in 2008, which may still works but doesn't take care of Virtualized TreeViews. Moreover many other problems are mentioned in comments of that article. No offences, but I am also stuck with same problem and can't find a perfect solution. Here are the links to some of the articles/posts which helped me a lot-

How can I expand items in a TreeView? – Part III: http://bea.stollnitz.com/blog/?p=59

Programmatically Selecting an Item in a TreeView: http://blog.quantumbitdesigns.com/2008/07/22/programmatically-selecting-an-item-in-a-treeview/#respond

TreeView, TreeViewItem and IsSelected: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7e368b93-f509-4cd6-88e7-561e8d3246ae/

akjoshi