2
votes

I've been looking for a solution to this problem but I still can't find an adequate solution yet. Please help. Basically, I have a treeview that's bound to a list of objects. When the user clicks on a treeviewitem, I load data for a data grid based on the treeviewitem. The user can change data in the grid. If there's a change in the data grid and the user clicks on another treeviewitem, the user is asked to save. The user can choose Yes, No, or Cancel. When the user chooses cancel, the application has to make the user stay on the current treeviewitem until he/she saves the data grid. The problem is once the user has selected another treeviewitem, I can't make it so that the previous selection is selected again. There is no "SelectedItemChanging" event. WPF treeview only has SelectedItemChanged event. I'm trying multiple solutions but I always end up with an infinitely. Please offer any tips or advices.

Thanks, Minh

1
For this kind of thing, I usually include an IsSelected property on the (view model) objects being bound to the tree view. If you set up a two way binding between the TreeViewItem.IsSelected property and the (view model) object property, you can programatically select an item in the tree view. If the control that holds the datagrid has access to the (view model) instance (or can pass a command to it), you can programatically select on cancel.Dave Clemmer

1 Answers

1
votes

You could just use a private field in which you store the item which was selected last, just handle the SelectedItemChanged event and always update the field as required. Also as Dave Clemmer suggested having an IsSelected property to which you can bind helps with selecting items programatically.