首页 > 其他 > 详细

传智播客——数据绑定基础

时间:2015-09-07 14:18:16      阅读:232      评论:0      收藏:0      [点我收藏+]

数据绑定基础

Slider:进度条

<Slider Name = "slider1"...></Slider>

<TextBox Text = "{Binding Value, ElementName = slider1}"></TextBox>

 

写一个数据绑定的类:

  class Person

  {

  }

后台:

  Person p1 = new  Person();

  txtName.DataContext = p1;

  txtAge.DataContext = p1;

前端:

  <TextBox Text = "{Binding Name}"

  <TextBox Text = "{Binding Age}"

 

尽量不要直接操控控件,而是新建一个类,new一个实例,给要绑定的控件设定DataContext

txtName.DataContext  = p1;

<TextBox Text = "{Binding Name}"

 

INotifyPropertyChanged

<TextBox TextChange

 

//.net内置的接口

//数据绑定会检测DataContext 是否实现了INotifyPropertyChanged

//如果实现了,就会监听PropertyChanged得知属性变化。

class Person:INotifyPropertyChanged

{

  private int age;

  public int Age

  {

    get

    {

      return age;

    }

    set

    {

      this.age = value;

      if(PropertyChanged != null)

      {

        PropertyChanged(this, new PropertyChangedEventArgs("Age"));

      }

    }

}

 

传智播客——数据绑定基础

原文:http://www.cnblogs.com/moqingtong/p/4788600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!