2

I have a TreeView like this:

xaml:

<TreeView>
     <TreeViewItem Header="header" Name="mytreeViewItem" Selected="mytreeViewItem_Selected"/>
</TreeView>

xaml.cs:

mytreeViewItem.ItemsSource = new List<string>(){"a"};

When I select the TreeViewItem nameed a, it will trigger event mytreeViewItem_Selected. But when I select the TreeViewItem again, it wont trigger the event again. How can I trigger the Selected event when I select the TreeView every time?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Dragon
  • 487
  • 4
  • 17
  • What exactly do you mean by "[...]when I select the `TreeViewItem` again[...]". Are you clicking it again, while it's still selected? Are you clicking it after you deselected it? Because the `Selected` event will only fire, if it's just getting selected. – grexter89 Sep 16 '15 at 08:30

2 Answers2

1

You can use PreviewMouseDown event or something similar (PreviewMouseLeftButtonDown for example) according to your needs.

Giangregorio
  • 1,482
  • 2
  • 11
  • 12
0

Can you have

item.IsSelected = false;

in your event handler? So when you click again, the Selected event re-fires. I don't think it will fire if the same is still selected.

PScr
  • 449
  • 2
  • 11
  • Sorry. I found this: http://stackoverflow.com/questions/5389797/unselecting-and-reselecting-a-treeviewitem-in-a-treeview, which might be a workaround for you, and this: http://stackoverflow.com/questions/491111/how-to-deselect-all-selected-items-in-a-wpf-treeview-when-clicking-on-some-empty. – PScr Sep 16 '15 at 08:45