EDIT: I’ve moved this page to my website.
This entry was posted on Friday, April 20th, 2007 at 1:53 pm and is filed under .Net 3.0, C#, Programming, WPF. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
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.
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.
What about the IsSelected property. Could you use it instead?
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.
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.
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.
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, etc. so what would I use?
Thanks a lot!
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?
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?
If you want to select it visually – use the code that has the strikethrough applied to it.
I had the same problem with my TreeView. I solved it like on this site:
This is simple and working!
It doesn’t work.
The ContainerFromItem returns null
Please log in using one of these methods to post your comment:
You are commenting using your WordPress.com account. ( Log Out / Change )
You are commenting using your Twitter account. ( Log Out / Change )
You are commenting using your Facebook account. ( Log Out / Change )
You are commenting using your Google+ account. ( Log Out / Change )
Connecting to %s
Notify me of new comments via email.
Blog at WordPress.com.