黑马程序员---wpf学习笔记四---banding的那些事

------- Windows Phone 7手机开发、 .Net培训、期待与您交流! -------
 
一、 WPF , 对控件做数据 banding , 可以方便的进行赋值和获得值的操作 :

例如一 :
< Grid > 
                 < Slider Name ="Slider1" HorizontalAlignment ="Left" Margin ="38,55,0,0" VerticalAlignment ="Top" Height ="19" Width ="227" /> 
                 < TextBox Text ="{Binding Value,ElementName=Slider1}" HorizontalAlignment ="Left" Height ="23" Margin ="38,101,0,0" TextWrapping ="Wrap" VerticalAlignment ="Top" Width ="120" />
</ Grid >
显示结果 :( 可以通过 slider 来滑动,控制 textbox 值的显示 )
例二 :  为控件设置数据源 :
 
1 .a  New 一个类的实例,给 要绑定的控件设定 DataContext
   private void Window_Loaded( object sender, RoutedEventArgs e)
  {
                        Person person = new Person();
                        person.Age = 22;
                        person.Name = "Charles";
                        tbAge.DataContext = person;
                        tbName.DataContext = person;
 }
  public class Person
        { 
                 public int Age { set; get; }
              public string Name { set; get; }
        }
 
1 . b    XAML 中设置数据绑定的控件属性
     < TextBox Name ="tbName" ToolTip ="{Binding Name}" HorizontalAlignment ="Left" Height ="23" Margin ="80,162,0,0" TextWrapping ="Wrap" Text ="{Binding Name}" VerticalAlignment ="Top" Width ="120" />
                 < TextBox Name ="tbAge" ToolTip ="{Binding Age}" HorizontalAlignment ="Left" Height ="23" Margin ="80,207,0,0" TextWrapping ="Wrap" Text ="{Binding Age}" VerticalAlignment ="Top" Width ="120" />
                         < TextBlock Text ="姓名" Width ="30" Margin ="39,163,434,125" />
                         < TextBlock Margin ="38,207,435,81" Text ="年龄" Width ="30" />

显示效果:
 
   
      但是 , 此种做法 , 无法将从后台变化的值在界面体现 ; 如果需要后台对象值变化 , 前台也变化 , 就使用【 INotifyPropertyChanged 】”属性变化通知”机制来实现 .
  2.a
修改类的定义 ( 注意需要引入 System.ComponentModel )
定义的类,需要继承 INotifyPropertyChanged ,并实现该接口方法,如下:
引入 : using System.ComponentModel;
class Person : INotifyPropertyChanged
     {
                 private int age;
                 private string name;
                 public int Age
                {
                        get { return age; }
                        set
                        {
                             this.age = value;
                                 if (PropertyChanged != null)
                                {
                                        PropertyChanged( this, new PropertyChangedEventArgs( "Age"));
                                }
                        }
                }
                 public string Name
                {
                        get { return name; }
                        set {
                                 this.name = value;
                                 if (PropertyChanged != null)
                                {
                                        PropertyChanged( this, new PropertyChangedEventArgs( "Name"));
                                }                                        
                        }
                }
                 public event PropertyChangedEventHandler PropertyChanged;
}
      其他步骤同上述 1.a\1.b, 为看到效果,可以添加一个按钮来实现!
                     private void btAge_Click( object sender, RoutedEventArgs e)
                {
                        person.Age++;
                }

截图:
二、给单个控件逐一赋值还是给所有控件共有的父控件赋值(进行数据 banding )之争:
     对控件上下文 ( DataContext ) 的指定 , 第一种 : 可以直接指定给控件 , 缺点是当控件多了 , 代码量相对来说就多来了 ; 第二种 : 指定给其父控件 , 当不给子控件指定 DataContext , 默认使用父控件的上下文 , 相对来说 , 代码简约 .
如下 :
采用第一种 :
                        Person person = new Person();
                        person.Age = 22;
                        person.Name = "Charles";
                        tbAge.DataContext = person;
                        tbName.DataContext = person;

采用第二种 :
Person person = new Person();
this.DataContext = person;(注意: this,指代当前窗体,也可以使用当前需要绑定上下文的共有父控件,如grid1.DataContext=person)
 
------- Windows Phone 7手机开发、 .Net培训、期待与您交流! -------
 
 
 
banding的其它学习,可以参考如下:
http://www.codesky.net/article/200808/130118.html
http://developer.51cto.com/art/200808/87103_4.htm

你可能感兴趣的:(学习,WPF,banding)