views:

271

answers:

1

I'm trying to write an event handler that fires every time a node in a TreeView gets the focus. The problem I'm running into is that the event handler fires on the TreeViewItem (node) that I click on with the mouse, and then it continues to bubble up the control tree, even though I've set e.Handled = true on the RoutedEventArgs provided to the handler. Does anybody have an idea what the problem could be ? I've double checked my code and I can see no reason why this should be happening.

+1  A: 

Are you using TreeView.GotFocus when you really want TreeViewItem.Selected?

    <TreeView TreeViewItem.Selected="treeView1_Selected"  />

If you really want focus, use TreeViewItem.Focus instead so that items are targeted instead of the whole tree.

    <TreeView TreeViewItem.GotFocus="treeView1_GotFocus"/>
Robert Jeppesen
I was using focus when I should have been using selected, but I'm still curious : why was the focus event bubbling up even though I had marked the RoutedEventArgs as handled ?
Alex Marshall
RoutedEvents continue to bubble up even when you mark them as handled. Any subscriber can choose to receive handled events too. If you're working with WPF a lot, this is a good read: http://msdn.microsoft.com/en-us/library/ms747183.aspx
Robert Jeppesen