简体   繁体   中英

IsChanged with IChangeTracking Not Firing on Window Close WPF MVVM C#

My goal is to show a messagebox on a window if the user tries to close the window and changes have been made to the fields. I am using wrappers to wrap the objects (inheriting the NotifyDataErrorInfoBase interface) that are represented on the screen, and all works fine when using command bindings and closing behaviors on the window closing events.

My problem is when the window first opens, a textbox has focus first. If I type something into the textbox, and then click the "X" button to close the window, the track changes does not work.

If I type something into the textbox and then click my Close button (which is bound to a closing command), it works fine.

If I select a value from a dropdown and then click the "X" button to close the window, it works fine.

It seems that tracking changes only works when data is confirmed to a field when focus is lost on a field or another action is conducted, like a button click. Is this true? If so, is it possible to start tracking changes on TextChanged ?

Try to set the UpdateSourceTrigger of the binding to PropertyChanged :

<TextBox Text="{Binding YourProperty, UpdateSourceTrigger=PropertyChanged}" />

This should cause the source property to get set immediately when you type something into the TextBox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM