语言类型:c#;
项目类型:WPF;
使用场景:1.
使用方式:1.使用时继承此类;
2.在VM的属性中的编码如下:
1. { set { this.OnPropertyChanged(()=>PropertyName); } }
2. { set { this.OnPropertyChanged("PropertyName"); } }
3. { set { this.OnPropertyChanged(nameof(Property)); } }
主要代码如下:
/// <summary>
/// Mvvm数据对象的基类
/// </summary>
public class BasicMvvmObject : System.ComponentModel.INotifyPropertyChanged
{
/// <summary> 在属性更改时发生 </summary>
public virtual event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 属性值变化时发生
/// </summary>
/// <param name="propertyName">属性名称</param>
protected virtual void _OnPropertyChanged(string propertyName)
{
this.PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// 属性值变化时发生
/// </summary>
/// <param name="propertyExp">指向的表达式</param>
protected virtual void _OnPropertyChanged<T>(Expression<Func<T>> propertyExp)
{
var _propertyName = (propertyExp.Body as MemberExpression).Member.Name;
this._OnPropertyChanged(_propertyName);
}
}