Click here to Skip to main content
15,887,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 );
        // The unsaved-changes flag gets set in here.
    }
}


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.)
Posted
Comments
[no name] 23-May-17 12:59pm    
http://www.wpf-tutorial.com/data-binding/the-update-source-trigger-property/
Patrick Skelton 23-May-17 13:07pm    
Arghh! How come I didn't know about that? (Please don't answer that.) Thank you so much. Screen now works perfectly!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900