zoukankan      html  css  js  c++  java
  • Tips: Save some typing when binding values to UI in WPF/Silverlight

    As we all know, In WPF or Silverlight application, if we want to update the value in the UI when the underlying data has changed, the class need to implement the "INotifyPropertyChanged” interface, which contains one delegate "PropertyChangedEventHandler”.

    So create a new method, and take the incoming parameter and pass it to the event argument, make sure call the method every time when the property has changed. just like the standard textbook said.

    How about create that method this way:

    Import System.Runtime.CompilerServices namespace.

     

            public event PropertyChangedEventHandler PropertyChanged;
     
            private void OnPropertyChanged([CallerMemberName]string caller = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(caller));
                }
            }

     

    With [CallerMemberName] attribute, you don’t have to explicit pass in the property name anymore, just call it in the set part, Framework will take care of the rest. like this:

            private string _foo;
            public string Foo
            {
                get
                {
                    return _foo
                }
                set
                {
                    _foo= value;
                    OnPropertyChanged();
                }
            }

    It’s not some big or fancy things, but maybe your keyboard will live longer. Fingers crossed

    Happy Coding Everyday~ 快乐编码,享受生活~
  • 相关阅读:
    iOS企业证书网页分发全过程具体解释(图文并茂史无前例的具体哦)
    MySql按周/月/日分组统计数据的方法
    Linux
    video_capture模块分析
    Go语言核心之美 1.1-命名篇
    《JAVA程序设计》实训第二天——《猜猜看》游戏
    openssl之EVP系列之10---EVP_Sign系列函数介绍
    从字节码指令看重写在JVM中的实现
    Dalvik虚拟机垃圾收集(GC)过程分析
    call to OpenGL ES API with no current context 和Fatal signal 11
  • 原文地址:https://www.cnblogs.com/tedzhang/p/3132007.html
Copyright © 2011-2022 走看看