WPF: stop or change a selection in a list box

Imagine: you have a Master-Child window, consisting of a list of elements (main window) and a set of controls where you can edit the currently selected element (child window). In the child window there are "Apply" and "Cancel" buttons.

The user begins to edit the values. He then changes the selection before clicking the "Apply" button.

Your application displays the message "Apply changes to the current item?", With the buttons "Yes", "No" and "Cancel". If the user clicks "Cancel", an attempt to change the current selection will fail.

The CurrentSelection element is a data binding.

I thought I could handle this in the "setter" part of the CurrentSelection property. If the user selects "Cancel", I simply save the CurrentSelection element as it is and fire the PropertyChanged notification event to tell the form to return to the old selected element. The control ignores this notification event. (Which makes sense, Control says: "I know that the current selection has changed, I just changed it!")

Any ideas how to fix this? So the control is trying to change the SelectedItem binding, and I want to say "No, you cannot change this selected item right now."

+6
data-binding wpf listbox
source share
3 answers

And to do what Kent Boogaart pointed out, refer to this answer.

How to stop WPF binding from ignoring the PropertyChanged event that it raised?

+3
source share

Just a thought without testing it: try adding the modified property event to a separate message. The list probably has a latch to ignore any notifications by changing the value of the property. If you send a separate message containing a notification, the latch must be reset and it must handle it.

+1
source share

This is similar to what you are looking for a memory pattern:

http://en.wikipedia.org/wiki/Memento_pattern

Hope this helps.

+1
source share

All Articles