TreeViewItem isSelected fired twice

1.9k views Asked by At

I have a TreeView with various items. the Items are styled such that the IsSelected property is bound to my VM to a bool IsSelected.

whenever I click a tree view item, this VM property is called twice, first with value==false then with value==true.

I assume this is a normal behavior, but I am not sure why, given that I have the IsExpended property bound in the same way, and is only called once.

Thanks

2

There are 2 answers

0
RichTea On BEST ANSWER

I suspect the reason is that when you select an item, first the old item is deselected, then the new item is selected. This results in a first call of false, and a second of true.

Perhaps while debugging, check a DisplayName or similar property on your view model, to see which items are being modified each time, to see if this is indeed the case.

Likewise when you expand a node, there's no need for any other nodes to be collapsed. Therefore, it simply expands the node you tried to expand (passing true for IsExpanded).

0
Ofir On

The reason for this is bubbling events.

When an event is raised, it will "bubble" up until it handled.

you can validate it by selecting a third level item.

You just need to set e.Handled = true;

Ofir