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.

Alex Marshall
  • 10,162
  • 15
  • 72
  • 117

1 Answers1

3

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
  • 7,837
  • 3
  • 35
  • 50
  • 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 Nov 25 '09 at 22:58
  • 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 Nov 25 '09 at 23:12
  • This answer, with this http://stackoverflow.com/questions/1127614/preventing-wpf-treeviews-selecteditemchanged-event-from-bubbling Solved my problem. Thank you all. – Tony Jun 06 '11 at 15:16