views:

452

answers:

3

If I have a databound form, how do I know if a user has modified it (either by typing text into a text box, or by selecting an item in a combobox)? I've tried hooking into my textbox's "TextChanged" event, but the problem is that, when my form participates in databinding, the "TextChanged" event fires, prematurely marking my form as "dirty."

A: 

When you first display the page, store the form values in an array, when the TextChanged event fires, compare to what you have already got, if it is different dirty form.

SeanJA
+1  A: 

Does your model class implement INotifyPropertyChanged? If so, you could add a handler for the PropertyChanged event on the class, and watch for the property in question to change.

If you haven't implemented INotifyPropertyChanged, maybe the mechanism that you're using to notify the UI layer of updates could be used here as well?

Andy
+3  A: 

try implementing

public partial class Window1 : INotifyPropertyChanged

and then

public event PropertyChangedEventHandler PropertyChanged;

public string UserName
{
    get { return _UserName; }
    set { if (value != _UserName)
    {
        _UserName = value;
        OnNotifyPropertyChanged("UserName");
    }}
}

private void OnNotifyPropertyChanged(string property)
{
  if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs(property));
}

and databind like

<TextBox Text="{Binding UserName}"/>
Jon Masters
Very minior improvement: you can reduce the nesting by instead making it:if (value == _UserName) return;Cuts down on the visual clutter a tiny bit.
Josh Kodroff