I have what I would consider to be a fairly 'normal' MVVM WPF application. I have a view which uses data-binding to a bunch of properties in the view-model. The properties in the view-model follow the usual pattern:
public string Name
{
get
{
return _name;
}
set
{
SetProperty( ref _name, value );
}
}
I have just noticed that if I open the view, type in a
TextBox and then immediately click the window-close button, it does not prompt me to save. This is because the
set code shown above is called by the data-binding engine only when the
TextBox loses focus.
Worse, unless I reload the list of objects in the view-model to which the view is bound, the data change I made above stays in the in-memory data object. This is because, when I close the window, the data-binding engine does realise that a change has been made and calls the
set code above, but this occurs
after I catch the window's
Closing event. This means my usual check for changes in the handler for the window's
Closing event does not know about the changes so does not prompt the user to save them.
I don't believe any of these three solutions I can think of is possible:
1. Get the data-binding engine to call the
set code every time a key is pressed inside a
TextBox;
2. Get the data-binding engine to call the
set code
before my window-closing event handler;
3. Find another window-closing event that occurs
after the data-binding engine has called the
set code.
The WPF technology is so widely used that I can't help thinking I am simply doing something wrong, because the current end-user feel is simply shabby.
Any suggestions would be gratefully received.
What I have tried:
I could handle the
KeyDown or
KeyUp event in every single
TextBox and use this to set the unsaved-changes flag, but this seems a bit clumsy. (Actually, I guess this would be pretty close to option #1 above.)