zoukankan      html  css  js  c++  java
  • wpf 属性变更通知接口 INotifyPropertyChanged

    在wpf中将控件绑定到对象的属性时, 当对象的属性发生改变时必须通知控件作出相应的改变, 所以此对象需要实现 INotifyPropertyChanged 接口

    例1: (注意:从 .Net 4.5 才开始支持此特性)

        //实现属性变更通知接口 INotifyPropertyChanged
        public class TestA : INotifyPropertyChanged
        {
            public long ID { get; set; }
    
            private string name;
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    if (value != this.name)
                    {
                        this.name = value;
                        //调用属性变更通知方法
                        NotifyPropertyChanged();
                    }
                }
            }
    
            // INotifyPropertyChanged 接口成员
            public event PropertyChangedEventHandler PropertyChanged;
            //此方法在每个属性的set中调用
            //在可选参数propertyName上应用CallerMemberName特性
            //调用此方法时,调用方的信息将替换为可选参数的参数,即在set中调用此方法,propertyName="当前属性的名称"
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
        }

     例2:(.net 4.0)

        //实现属性变更通知接口 INotifyPropertyChanged
        public class TestA : INotifyPropertyChanged
        {
            public long ID { get; set; }
    
            private string name;
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    if (value != this.name)
                    {
                        this.name = value;
                        //调用属性变更通知方法,参数为"属性名称"
                        NotifyPropertyChanged("Name");
                    }
                }
            }
    
            // INotifyPropertyChanged 接口成员
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
    
        }
  • 相关阅读:
    读书笔记——吴军《态度》
    JZYZOJ1237 教授的测试 dfs
    NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论
    [JZYZOJ 1288][洛谷 1005] NOIP2007 矩阵取数 dp 高精度
    POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数
    POJ2157 Check the difficulty of problems 概率DP
    HDU3853 LOOPS 期望DP 简单
    Codeforces 148D. Bag of mice 概率dp
    POJ3071 Football 概率DP 简单
    HDU4405 Aeroplane chess 飞行棋 期望dp 简单
  • 原文地址:https://www.cnblogs.com/gmcn/p/5882026.html
Copyright © 2011-2022 走看看