Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Stopping or reversing a selection change in a list box

Imagine this: you have a Master-Child window consisting of a list of items (the Master window) and a set of controls where you can edit the currently selected item (the Child Window). The child window has "Apply" and "Cancel" buttons.

A user begins editing values. He then changes the selection, before pressing the "Apply" button.

Your application displays a message "Apply changes to the current item?", with the buttons being "Yes", "No" and "Cancel". If the user presses "Cancel" then the attempt at changing the current selection should fail.

The "CurrentSelection" item is databound.

I thought I could deal with this in the "setter" part of the CurrentSelection property. If the user selects "Cancel", I simply keep the CurrentSelection item as it is, and fire a PropertyChanged notification event to tell the form to update back to the old selected item. The control is ignoring this notification event. (Which makes sense, the Control is saying "I know the current selection has changed. I just changed it!")

Any ideas how to fix this? In summary, the control tries to change the bound SelectedItem, and I want to tell it "No you can't change this selected item right now".

like image 206
Andrew Shepherd Avatar asked Sep 17 '09 08:09

Andrew Shepherd


3 Answers

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

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

like image 65
Trainee4Life Avatar answered Nov 15 '22 10:11

Trainee4Life


This sounds like you are looking for the memento pattern:

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

Hope this helps.

like image 27
Burt Avatar answered Nov 15 '22 08:11

Burt


Just a thought without having tested it: try raising the property changed event in a separate message. The list probably has a latch to ignore any notifications whilst it's changing the property value. If you dispatch a separate message that contains the notification, the latch should be reset and it should handle it.

like image 37
Kent Boogaart Avatar answered Nov 15 '22 09:11

Kent Boogaart