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."
如果我有一个databound表单,我如何知道用户是否修改了它(通过在文本框中输入文本或在combobox中选择项)?我尝试过连接到我的文本框的“TextChanged”事件,但问题是,当我的表单参与到databinding时,“TextChanged”事件会触发,过早地将我的表单标记为“dirty”。
4 个解决方案
#1
3
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
和databind
<TextBox Text="{Binding UserName}"/>
#2
1
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.
您的模型类实现inotifypropertytychanged了吗?如果是这样,您可以为类上的PropertyChanged事件添加一个处理程序,并监视要更改的属性。
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?
如果您还没有实现INotifyPropertyChanged,那么您用来通知UI层更新的机制也可以在这里使用吗?
#3
0
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.
当您第一次显示页面时,将表单值存储在一个数组中,当TextChanged事件触发时,如果它是不同的脏表单,则与您已经获得的值进行比较。
#4
0
--> http://www.mishainthecloud.com/2009/07/simple-dirty-tracking-for-winforms-in-c.html.
- - > http://www.mishainthecloud.com/2009/07/simple-dirty-tracking-for-winforms-in-c.html。
I know this is a bit late but I like this approach :) Encapsulated goodness.
我知道这有点晚了,但我喜欢这种方法:)概括的好。
After binding the controls, call SetAsClean(). This method call could probably be encapsulated in the same dirty-tracking-class by handling some of the BindingSources' events.
绑定控件之后,调用SetAsClean()。通过处理绑定源的一些事件,可以将这个方法调用封装在同一个脏跟踪类中。
#1
3
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
和databind
<TextBox Text="{Binding UserName}"/>
#2
1
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.
您的模型类实现inotifypropertytychanged了吗?如果是这样,您可以为类上的PropertyChanged事件添加一个处理程序,并监视要更改的属性。
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?
如果您还没有实现INotifyPropertyChanged,那么您用来通知UI层更新的机制也可以在这里使用吗?
#3
0
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.
当您第一次显示页面时,将表单值存储在一个数组中,当TextChanged事件触发时,如果它是不同的脏表单,则与您已经获得的值进行比较。
#4
0
--> http://www.mishainthecloud.com/2009/07/simple-dirty-tracking-for-winforms-in-c.html.
- - > http://www.mishainthecloud.com/2009/07/simple-dirty-tracking-for-winforms-in-c.html。
I know this is a bit late but I like this approach :) Encapsulated goodness.
我知道这有点晚了,但我喜欢这种方法:)概括的好。
After binding the controls, call SetAsClean(). This method call could probably be encapsulated in the same dirty-tracking-class by handling some of the BindingSources' events.
绑定控件之后,调用SetAsClean()。通过处理绑定源的一些事件,可以将这个方法调用封装在同一个脏跟踪类中。