silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight中的数据绑定(1)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

数据绑定在Silverlight中由Binding类实现。Binding类有2个组成部分,source和target,还有一个定义两者绑定方式的属性叫做binding mode,mode定义了数据怎么样在source和target之间传递(one-way,one-time,two-way)。 定义控件的绑定属性,需要使用XAML标记,比如{Binding <path>}.加上要绑定数据源的Firs

数据绑定在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" Name="textBox1" VerticalAlignment="Top" ;120" Text="{Binding namE}" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="51,60,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" 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" Name="slider1" VerticalAlignment="Top" ;100" Value="100" Maximum="1000" />

 

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

大佬总结

以上是大佬教程为你收集整理的Silverlight中的数据绑定(1)全部内容,希望文章能够帮你解决Silverlight中的数据绑定(1)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。