We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

TreeViewAdv undo selection change in code

Hello

In my project I use the TreeViewAdv control to switch between different projects items which can be edited by the user but before switching I need to validate the input and if a validation error occurs I need to prevent the switch.

As there is no event in TreeViewAdv which allows me to cancel the selection of a new item I instead try to revert the selection back to it's previous state.

In my OnSelectedItemChanged code I try:

myTree.SelectedItems.Clear();
myTree.SelectedTreeItem = oldSelItem;

which causes another SelectedItemChanged event as expected, but after I ignore this and set e.Handled = true I receive another SelectedItemChanged event which has an SelectedItems.Count of 2 and SelectedItems[0] is the oldSelItem and SelectedItems[1] contains the item the user selected previously Again I have to clear the SelectItems and set it to oldSelItem to finally have my old selection back but this behaviour is very strange and complicate to prevent a selection change.

Here the sequence of the events:

TreeViewAdv with current selected item id: 3070

User clicks on item with id 3153 and causes SelectedItemChanged event with item id 3153

Validation of current item (3070) fails and needs to prevent the selection change:

myTree.SelectedItems.Clear();
myTree.SelectedTreeItem = oldSelItem;

SelectedItemChanged event with item id 3070 (as expected)

e.Handled = true;

return;

SelectedItemChanged event with item id 3070 and 3153 (unexpected)

here I need

myTree.SelectedItems.Clear();
myTree.SelectedTreeItem = oldSelItem;

again

SelectedItemChanged event with item id 3070 (as expected) to finally change back to the old item.

Is there a better way to handle this and what causes this strange SelectedItemChanged event?

Regards,

Reinhard


4 Replies

RS Reinhard Schürer January 24, 2014 05:13 PM UTC

Somehow this post was posted twice and I can't find a possibility to delete a post.



MK Markus Klassen January 29, 2014 10:45 AM UTC

Since I've just had to deal with the Treeview control as well (annoying control in general, not Syncfusion's fault). I am using MVVM but I am sure it's not that much different. Basically you need handle the events of the TreeViewItemAdv control (could be PreviewMouseDown and PreviewKeyDown) and set handled = true. Trying the same events of the TreeViewAdv control is just super annoying and will basically not work without tons of trouble.


MK Markus Klassen January 29, 2014 10:57 AM UTC

Can't edit posts, I mean the normal non preview events (MouseDown, KeyDown).


JR Jegan Raj M Syncfusion Team February 4, 2014 04:02 AM UTC

Hi Reinhard,

In our current implementation of 'TreeViewAdv' we don't have support for 'SelectionChanging' event to cancel the selected event. If we do work around for this requirement, it will call 'SelectedItemChanged' recursively when we cancel selection. So, we consider this as a feature request and added to our database. Please visit our website periodically for feature related updates.

Please let us know if you need further assistance.

Regards,
Jegan Raj M

Loader.
Live Chat Icon For mobile
Up arrow icon