zoukankan      html  css  js  c++  java
  • 传智播客--数据绑定--INotifyPropertyChanged(小白内容)

    INotifyPropertyChanged一般在数据绑定的时候使用。

    InotifyPropertyChanged是.net内置的接口,数据绑定时会检测DataContext是否实现了InotifyPropertyChanged,如果实现了,就会监听PropertyChanged,得知属性变化。

    可以理解为InotifyPropertyChanged接口用于向客户端发出某一属性值已更改的通知。

    class Person:INotifyPropertyChanged
        {
            private int age;
            public int Age
            {
                get
                {
                    return age;
                }
    
                set
                {
                    this.age = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                    }
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
        }

    .cs文件

     public partial class MainWindow : Window
        {
            private Person p = new Person();
            public MainWindow()
            {
                InitializeComponent();           
    
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                p.Age++;
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                p.Age = 10;
                txtAge.DataContext = p;
            }
        }

    xaml文件

    <TextBox Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="82,39,0,0" TextWrapping="Wrap" Text="{Binding Age}" VerticalAlignment="Top" Width="120"/>
    <Button Content="Age++" HorizontalAlignment="Left" Margin="271,39,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

    这样点击button可以实现textbox里的数字自增。

  • 相关阅读:
    es6---let和const
    node.js开发指南系列(1)partial is not defined
    input唤起键盘影响移动端底部fixed定位
    vue滑动吸顶以及锚点定位
    nodejs开发准备工作(2)
    nodejs开发准备工作(1)
    php基础小知识
    php基础
    git基础
    ps基础
  • 原文地址:https://www.cnblogs.com/ZXdeveloper/p/4103701.html
Copyright © 2011-2022 走看看