Inotifyproperty改变了全球使用 [英] Inotifypropertychanged global use
本文介绍了Inotifyproperty改变了全球使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在WinForms应用程序中,只有在用户更改了内容时才能保存。
为此,我在所有实体类中实现了INotifyPropertyChanged。 br />
现在我想在存储之前知道全局是否在任何领域发生了变化。
什么是最好的方法?
谢谢
LG
妮可
Hi,
In a WinForms application should only be saved if the user has changed something.
To do this, I implement the INotifyPropertyChanged in all entity classes.
Now I want to know globally before storing whether something has changed in any field.
What is the best method?
thank you
LG
Nicole
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
}
我尝试了什么:
What I have tried:
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
}
推荐答案
一种方法是创建一个可重用的基类持有状态的字段或属性。
考虑以下示例
One way could be to create a reusable base class which holds a field or property for the status.
Consider the following example
public class SomeBase {
public event PropertyChangedEventHandler PropertyChanged;
private bool _changed;
public readonly List<string> ChangedProperties;
public bool Changed {
get {
return this._changed;
}
set {
if (this._changed != value) {
this._changed = value;
// Reset the list if not changed
if (!this._changed) {
this.ChangedProperties.Clear();
}
}
}
}
public SomeBase() {
this.Changed = false;
this.ChangedProperties = new List<string>();
}
public void NotifyPropertyChanged(string propertyName) {
PropertyChangedEventHandler eventHandler = PropertyChanged;
this.Changed = true;
if (eventHandler != null) {
eventHandler(this, new PropertyChangedEventArgs(propertyName));
if (!this.ChangedProperties.Contains(propertyName)) {
this.ChangedProperties.Add(propertyName);
}
}
}
}
public class MyClass : SomeBase {
private string customerNameValue;
public string CustomerName {
get {
return this.customerNameValue;
}
set {
if (value != this.customerNameValue) {
this.customerNameValue = value;
NotifyPropertyChanged("CustomerName");
}
}
}
}
现在获取数据后,设置将更改状态设置为false,然后您可以检查更改的状态。
Now after fetching the data, set the Changed status to false and after that you can check the status for changes.
这篇关于Inotifyproperty改变了全球使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文