Silverlight中的数据绑定(1)

数据绑定在Silverlight中由Binding类实现。Binding类有2个组成部分,source和target,还有一个定义两者绑定方式的属性叫做binding mode,mode定义了数据怎么样在source和target之间传递(one-way,one-time,two-way)。

定义控件的绑定属性,需要使用XAML标记,比如{Binding <path>}.加上要绑定数据源的FirstName元素到Text属性。那就如下写法:<TextBox Text="{Binding FirstName }" />。其中Binding FirstName 等于Binding Path=FirstName

Binding类参考http://msdn.microsoft.com/zh-cn/library/ms617928.aspx

相关语法语句参照如下:

<TextBox Height="23" HorizontalAlignment="Left" Margin="51,17,0,0" Name="textBox1" VerticalAlignment="Top" ;120" Text="{Binding name}" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="51,60,0,0" Name="textBox2" VerticalAlignment="Top" ;120" Text="{Binding age}" />

student s = new student()
                   {
                       name = "zhang",
                       age = 20
                   };

private void button1_Click(object sender, RoutedEventArgs e)
       {
           this.LayoutRoot.DataContext = s;
       }

这些语句可以是绑定的数据自动赋上值。

DataContext 属性可以直接设置在Control上。如果绑定的控件上未设置DataContext属性,那就会去控件的父级控件寻找DataContext。好处是这种this.LayoutRoot.DataContext = s;语句,在这个页面的控件都可以使用该数据源。

 

绑定的时候,有3中选择:

OneTime模式下:控件与数据绑定后,能自动显示数据,一旦显示完成后,这二者就没有任何关联了。(即自动解除绑定)
OneWay模式下:控件与数据绑定后,除自动显示数据外,显示完成后,控件与数据源仍有单向关联,即如果数据源以后发生了变化,控件上的值也会自动变化.
TwoWay模式下:基本与OneWay相同,但是显示完成后,控件与数据源的关联是双向的,即数据源的变化会影响控件上的值,反过来控件上的任何值变化也会影响数据源本身发生变化。

对一个实体,想实现对这个实体的TwoWay变化,并且变化的结果可以显示在OneWay绑定的TextBlock上,看如下代码:

public BasicDataBinding()
       {
           InitializeComponent();

           //s.PropertyChanged += (sender, e) =&gt; {
           //    MessageBox.Show("changed");
           //};

           Binding bdname = new Binding("name");//这些代码都可以不用写,直接通过属性设置。
           bdname.Mode = BindingMode.TwoWay;
           Binding bdage = new Binding("age");
           bdage.Mode = BindingMode.TwoWay;

           this.textBox3.SetBinding(TextBox.TextProperty, bdname);
           this.textBox4.SetBinding(TextBox.TextProperty, bdage);
           this.textBox3.DataContext = s;
           this.textBox4.DataContext = s;

           //----------------------------------------------------------------

           bdname = new Binding("name");
           bdname.Mode = BindingMode.OneWay;
           bdage = new Binding("age");
           bdage.Mode = BindingMode.OneWay;

           this.textBlock11.SetBinding(TextBlock.TextProperty, bdname);
           this.textBlock12.SetBinding(TextBlock.TextProperty, bdage);
           this.textBlock11.DataContext = s;
           this.textBlock12.DataContext = s;

       }
       public student s = new student()
                  {
                      name = "zhang",
                      age = 20
                  };

 

如果要实现联动,即s的值变了,textBlock11的值也变。那么对于student类需要实现接口。并且对于set属性的方法要修改一下。具体代码如下:

public class student : INotifyPropertyChanged
  {
      private string _name;
      private int _age;
      public string name
      {
          set
          {
              _name = value;
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs("name"));
          }
          get
          {
              return _name;
          }
      }
      public int age
      {
          set
          {
              _age = value;
              if (PropertyChanged != null)
                  PropertyChanged(null, new PropertyChangedEventArgs("age"));
          }
          get
          {
              return _age;
          }
      }

      public event PropertyChangedEventHandler PropertyChanged;
  }

 

对于和相关元素绑定的,似乎更简单,不需要实现什么INotifyPropertyChanged接口,直接属性里面设置就行了。

示例代码如下:

<TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="32,11,0,0" Name="textBox1" VerticalAlignment="Top" ;120" Text="{Binding Path=Value, Mode=TwoWay, ElementName=slider1}" />括号里的值可以通过属性窗口设置。
        <Slider Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="48,85,0,0" Name="slider1" VerticalAlignment="Top" ;100" Value="100" Maximum="1000" />

 

另:此处还可以参考http://www.cnblogs.com/yjmyzz/archive/2009/11/09/1599058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值