[英]When do I need to use automatic poperties and when properties with propertychanged event?
I am using wpf and its C sharp! 我正在使用wpf及其C语言!
I have this in my Animal.cs clas 我的Animal.cs clas中有这个
private string _animalName;
public string AnimalName
{
get { return _animalName; }
set
{
if(_animalName!= value)
{
_animalName= value;
this.NotifyPropertyChanged("AnimalName");
}
}
}
I could also write: 我也可以写:
public string AnimalName {get;set;} 公共字符串AnimalName {get; set;}
There is no difference in binding and validation. 绑定和验证没有区别。 Everythings works as before when I exchange the code.
交换代码时,Everything仍像以前一样工作。
Is this due to the fact that I only create new animals but I do not allow to update the animals name in my application ? 是否由于我只创建新动物而不允许在应用程序中更新动物名称的事实?
So I need to call the propertyChanged("AnimalName"); 所以我需要调用propertyChanged(“ AnimalName”); only when I want to change its property value?
仅当我想更改其属性值时?
I am ac# beginner ;) 我是ac#初学者;)
If your object has an updateable property (setter) that will be bound to a control then you need to ensure to let the bound control know of any changes to that property via INotifyPropertyChanged. 如果您的对象具有将绑定到控件的可更新属性(setter),则您需要确保通过INotifyPropertyChanged使绑定的控件知道对该属性的任何更改。 However, if you have a readonly property and/or a property that's not going to be used in a data-binding scenario then you don't care about implementing or calling NotifyPropertyChanged method from within that property's setter in which case you can use automatic properties.
但是,如果您具有只读属性和/或不打算在数据绑定方案中使用的属性,那么您就不必在该属性的设置器内实现或调用NotifyPropertyChanged方法,在这种情况下,您可以使用自动属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.