所有的重载设置字段作为参数传递到指定的值,而属性提高INotifyPropertyChanged.PropertyChanged事件。
如果一个字段已经成功地改变,setProperty方法中返回真。一些重载也需要一个回调方法作为参数。调用该回调后场已经改变。
- 手动设置一个基本的私有字段,然后调用的BindableBase.RaisePropertyChanged方法重载之一。
所有RaisePropertyChanged方法重载需要作为输入参数,这些属性提高INotifyPropertyChanged.PropertyChanged事件。
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Xpf.Mvvm; public class BindableObject : BindableBase { //The property's setter below uses the SetProperty method, which does the following:
//1. Changes the underlying private field.
//2. Raises a change notification for the property, whose name is retrieved from the expression: () => StringProperty.
//This is the preferable method for setting a property in most cases, because it provides strong typing.
//The method is slightly slower than passing the property name as a string (see the declaration of StringProperty2).
string stringProperty;
public string StringProperty {
get { return stringProperty; }
set { SetProperty(ref stringProperty, value, () => StringProperty); }
} //The property's setter below uses the SetProperty method, which does the following:
//1. Changes the underlying private field.
//2. Raises a change notification for the property with the specified name.
//This method is slightly faster than the previous variant, but does not provide strong typing.
string stringProperty2;
public string StringProperty2 {
get { return stringProperty2; }
set { SetProperty(ref stringProperty2, value, "StringProperty2"); }
} //The property's setter below uses the SetProperty method, which does the following:
//1. Changes the underlying private field.
//2. Invokes your callback method (OnStringProperty3Changed) if the property has been changed.
//3. Raises a change notification for the property, whose name is retrieved from the expression: () => StringProperty3.
string stringProperty3;
public string StringProperty3 {
get { return stringProperty3; }
set {
SetProperty(ref stringProperty3, value, () => StringProperty3, OnStringProperty3Changed);
//An alternative form:
SetProperty(ref stringProperty3, value, () => StringProperty3, () => OnStringProperty3Changed());
}
}
private void OnStringProperty3Changed() {
} //The property's setter below uses the SetProperty method, which does the following:
//1. Changes the underlying private field.
//2. Raises a change notification for the property, whose name is retrieved from the expression: () => StringProperty4.
//You can write additional code within the "if" statement, which will be executed after the property has changed.
string stringProperty4;
public string StringProperty4 {
get { return stringProperty4; }
set {
if (SetProperty(ref stringProperty4, value, () => StringProperty4)) {
//The SetProperty method returns true if the property has been changed.
///Do “something” after the property has changed.
//...
}
}
} //The following property's setter does the following:
//1. Changes the underlying private field.
//2. Raises a change notification via the RaisePropertyChanged method.
string stringProperty5;
public string StringProperty5 {
get { return stringProperty5; }
set {
if (stringProperty5 == value)
return;
stringProperty5 = value;
RaisePropertyChanged("StringProperty5");
//An alternative form using a lambda expression, which provides strong typing:
RaisePropertyChanged(() => StringProperty5);
//Do “something” after the property has changed.
//...
}
} //The following property's setter changes a property, and raises change notifications for this property and its dependent properties:
//1. Changes the underlying private field (stringProperty6).
//2. Raises change notifications for StringProperty and StringProperty2.
//3. Raises a change notification for StringProperty6.
//This is the preferable variant in most cases, because it features strong typing.
//The method is slightly slower than passing the property name as a string (see the implementation of StringProperty7).
string stringProperty6;
public string StringProperty6 {
get { return stringProperty6; }
set {
SetProperty(ref stringProperty6, value, () => StringProperty6, () => RaisePropertiesChanged(() => StringProperty, () => StringProperty2)); //An alternative form that is easier to read:
if (SetProperty(ref stringProperty6, value, () => StringProperty6)) {
RaisePropertiesChanged(() => StringProperty, () => StringProperty2);
}
}
} //The following property's setter changes the property and raises change notifications for this property and its dependent properties:
//1. Changes the underlying private field (stringProperty7).
//2. Raises a change notification for StringProperty7.
//3. Raises change notifications for StringProperty and StringProperty2.
//This is a quicker alternative to the previous example, but does not provide strong typing.
string stringProperty7;
public string StringProperty7 {
get { return stringProperty7; }
set {
if (SetProperty(ref stringProperty7, value, () => StringProperty7)) {
RaisePropertiesChanged("StringProperty", "StringProperty2");
}
}
}
}
该代码来自官网