zoukankan      html  css  js  c++  java
  • wpf学习笔记更新数据源

    此示例基于 wpf学习笔记-指定数据源


    1.让对象实现INotifyPropertyChanged接口,以便属性更改发出通知

        public class Person : INotifyPropertyChanged
        
    {
            
    public Person() { }
            
    public Person(string name, int age)
            
    {
                
    this.name = name;
                
    this.age = age;
            }


            
    string name;
            
    public string Name
            
    {
                
    get return this.name; }
                
    set
                
    {
                    
    this.name = value;
                    OnPropertyChanged(
    "Name");
                }

            }


            
    int age;
            
    public int Age
            
    {
                
    get return this.age; }
                
    set
                
    {
                    
    this.age = value;
                    OnPropertyChanged(
    "Age");
                }

            }



            
    public event PropertyChangedEventHandler PropertyChanged;

            
    protected void OnPropertyChanged(string propName)
            
    {
                
    if (this.PropertyChanged != null)
                
    {
                    PropertyChanged(
    thisnew PropertyChangedEventArgs(propName));
                }

            }


        }

    2.xaml(略去布局)


            
    <Label Content="{Binding Name}"></Label>
            
    <Label Content="{Binding Age}"></Label>
            
    <TextBox Text="{Binding Path=Name, Source={StaticResource Tom}}" />
            
    <TextBox Text="{Binding Age}" 
                
    />
    这里又出现了新的绑定语法,{Binding Path=Age}等价{Binding Age}

    3.目标:
    当更改目标属性的时候,更新数据源(更新以后则绑定的对象也发生变化,如更改TextBox的Text则Label的Content也发生变化)

    4.设置更新数据源执行时间
    通过设置Binding对象的UpdateSourceTrigger  来确定执行时间.

    根据需要设置UpdateSourceTrigger 属性

  • 相关阅读:
    4.22 每日一题题解
    4.21 每日一题题解
    4.20 每日一题题解
    【HDU2825】Wireless Password【AC自动机,状态压缩DP】
    【POJ2778】DNA Sequence 【AC自动机,dp,矩阵快速幂】
    【ZOJ 3228】Searching the String 【AC自动机】
    【LA5135 训练指南】井下矿工 【双连通分量】
    【LA3523 训练指南】圆桌骑士 【双连通分量】
    【LA3713 训练指南】宇航员分组 【2-sat】
    【LA3211 训练指南】飞机调度 【2-sat】
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1211173.html
Copyright © 2011-2022 走看看