How to programmatically change the SelectedItem in a WPF TreeView

EDIT: I’ve moved this page to my website.


12 Responses to How to programmatically change the SelectedItem in a WPF TreeView

  1. Alek Davis says:

    Alternatively, you could call Focus() on the tree view item you want to select, although this would move the focus to the tree view item, which may not be something you want to do.

  2. It’s my understanding that all Focus() does is grab keyboard focus…and it doesn’t even grab it in all instances. I think that changing the SelectedItem is the best way to go. It works every time, and is the most logical event handler for most people.

  3. Alek Davis says:

    What about the IsSelected property. Could you use it instead?

  4. The IsSelected property is specific to the TreeViewItem. Its scope doesn’t go beyond the child control. So changing the IsSelected property will only cause the TreeViewItem to raise it’s own “Selected” event. So if you had some kind of logic that you wanted bound the the selected object changing, I believe you’d have to bind an event handler to every single TreeViewItem — instead of the TreeView itself. Now, it’s been a while since I looked at this, but if the Selected event of the TreeViewItem were to call the Select method, then using the IsSelected property would be ideal — but I don’t think it does that. I think that the SelectedItemChanged event “causes” the Selected event to fire. I believe it’s kind of like the Focus method — all it does is change the TreeViewItem. You have to remember that most of these events are “bubbled” down from the TreeView itself, and that your ideal handling should be done on the SelectedItemChanged event. You have to force that event to fire.

  5. Alek Davis says:

    Hmmm, I’m not sure I’m following you. Are you saying that setting the IsSelected property on a tree view item to true, does not affect the tree? Such as it could cause the tree to have a bunch of items with the IsSelected property set to true? And it would not cause the SelectedItemChanged event on the tree? I don’t think this is the case. I just tried it in debug mode and after I make a call to set the IsSelected property to true, I can see that the tree raises the SelectedItemChanged event, so it looks to me like it works fine. Don’t know, maybe I misunderstood your point.

  6. Ha!…I was wrong. Like I said in my previous comment, I hadn’t looked at that stuff in a while. But, yes, setting the IsSelected property to true does cause the SelectedItemChanged event to fire. Hmm…I was convinced that something like that didn’t work before and I don’t know why. So instead of using Reflection to change the SelectedItem…it would have been alot easier (and smarter) to just set the IsSelected property to true once you get it from the ItemContainerGenerator.

  7. docmars says:

    Kinda stupid question.. but for usage of this method, what would I specify as my item?

    SetSelectedItem(ref TreeView control, object item)…

    … where I’m using: SetSelectedItem(ref myTreeView, …)
    What do I use in the “object item” argument? I tried myTreeView.Items[1], etc. so what would I use?

    Thanks a lot!

  8. chertby says:

    Everything workin great…. but when I try run this method for items that contains lower than first TreeViewItems (I try this method for complex TreeView) I recive error.
    What I am doing wrong?

  9. nejck says:

    ok, so using this method onselected event is raisen, but item is not selected visualy…
    Can anyone help me how to do programmatically the same thing as if I would click on the item?

  10. If you want to select it visually – use the code that has the strikethrough applied to it.

  11. hbdiere says:

    I had the same problem with my TreeView. I solved it like on this site:

    This is simple and working!

  12. weitzhandler says:

    It doesn’t work.

    The ContainerFromItem returns null

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: