1

What is the best method, to detect, wether datas has changed in a WPF-Window or not?

I've created a default window for our project, which contains basic saving and delete behaviour. But currently the window always asks, wether to save changes or not. But i want to implement, that the save-dialog only appears, if the user realy had changed some thing.

The first idea i had was, to create an interface, which must be implemented in all controls which were used in the window. But than i'm not able to simply use the wpf default controls.

Does anyone has some experiances with this approach?

Thank you!

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • 1
    Bit hard to tell how you are coding it from the question, but If you are using an MVVM framework, there is usually an "IsDirty" property that can be read to tell if the model has changed. A couple of potentially useful links - http://stackoverflow.com/questions/5890212/wpf-mvvm-how-to-detect-if-a-view-is-dirty and http://www.codeproject.com/Articles/695856/Lets-get-dirty – JsAndDotNet May 22 '15 at 07:58

1 Answers1

1

Personally, I like to solve this problem by subscribing to the Control's SourceUpdated event. You can use one event handler for all of your controls, which makes this very nifty in my eyes. In your XAML, use

<TextBox x:Name="MyNotifyingTextBox" Text="{Binding Path=SomeProperty, NotifyOnSourceUpdated=True}" SourceUpdated="Control_SourceUpdated"/>

And use some code like this in your code-behind / ViewModel / ...

bool hasChange = false;

public bool HasChange
{
    get { return hasChange; }
    set { hasChange = value; }
}

private void Control_SourceUpdated(object sender, DataTransferEventArgs e)
{
    this.HasChange = true;
}
Physikbuddha
  • 1,652
  • 1
  • 15
  • 30