【WPF】奇怪的INotifyPropertyChanged的实现

MSDN是这样解释的:

INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。

例如,考虑一个带有名为 FirstName 属性的 Person 对象。 若要提供一般性属性更改通知,则 Person 类型实现 INotifyPropertyChanged 接口并在 FirstName 更改时引发 PropertyChanged 事件。

这一种大家都明白,实现方法是这样:

 

public class Person : INotifyPropertyChanged
{
    private string firstNameValue;
    public string FirstName{
        get { return firstNameValue; }
        set
        {
            firstNameValue=value;
            // Call NotifyPropertyChanged when the property is updated
            NotifyPropertyChanged("FirstName");
        }
    }

    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;

    // NotifyPropertyChanged will raise the PropertyChanged event passing the
    // source property that is being updated.
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }  
}

不过我们可以把它封装起来,以便直接调用:我之前看到有两种实现方法,但不明白后者为什么要那样写,请各位指点:

第一种:

public class NotificationObject : INotifyPropertyChanged
 {
        public event PropertyChangedEventHandler PropertyChanged;
         private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
}

然后在我们自定义的类中使用:

public class Person : NotificationObject
{
    private string firstNameValue;
    public string FirstName{
        get { return firstNameValue; }
        set
        {
            firstNameValue=value;
            // Call NotifyPropertyChanged when the property is updated
            RaisePropertyChanged("FirstName");
        }
}

上面这一种经常使用,但下面这一种却不知道有什么好处,或者优点,用的是表达式树。

 

 public class NotificationObject : INotifyPropertyChanged
    {
        protected void RaisePropertyChanged<T>(Expression<Func<T>> action)
        {
            var propertyName = GetPropertyName(action);
            RaisePropertyChanged(propertyName);
        }

        private static string GetPropertyName<T>(Expression<Func<T>> action)
        {
            var expression = (MemberExpression)action.Body;
            var propertyName = expression.Member.Name;
            return propertyName;
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

调用的时候也是直接调用:不过这次传入的不是属性参数,而是一个表达式。

public class Person : NotificationObject
{
    private string firstNameValue;
    public string FirstName{
        get { return firstNameValue; }
        set
        {
            firstNameValue=value;
            // Call NotifyPropertyChanged when the property is updated
            RaisePropertyChanged(()=>Name);
        }
}

请各位指点。

转载于:https://www.cnblogs.com/wywnet/p/3503778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值