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
2009-03-10 02:56:31
+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
2009-03-10 02:59:16
+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
2009-04-25 03:09:42
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
2009-04-25 13:43:29