一个数据绑定可以通过 Binding 对象来描述,其中包含数据源,要绑定的属性路径(Path),目标,目标属性等。 其中目标属性必须是依赖属性(DependencyProperty)。 为了说明方便,首先定义一个数据类:

    public class Person{public int Age { get; set; }public string Name { get; set; }}
例子1:
        <ListBox x:Name="list1"></ListBox>
    public partial class Page : UserControl{public Page(){InitializeComponent();var persons = new List<Person>();for(var i=0; i< 5; i++){persons.Add(new Person {Name = "Person " + i.ToString(), Age = 20 + i});}list1.DataContext = persons;}}
这里仅指定了 list1 的 DataContext 属性,运行后发现页面没有显示。
如果在页面里改一改:
        <ListBox x:Name="list1" ItemsSource="{Binding}"></ListBox>

会发现绑定成功。但是数据项显示为默认的 Person 对象 ToString() 后的表示,不太友好。如下图:

或者,也可以将后台代码改成:

list1.ItemsSource = persons;

而页面 markup 仍然是:

        <ListBox x:Name="list1"></ListBox>

这样也能绑定成功。 这里的原因在于:ListBox 通过 ItemsSource 里的数据去填充数据项,所以直接给这个属性赋值是可以的。 或者,通过空绑定语法 {Binding},指定 ItemsSource 属性绑定为数据源的对象本身(未指定绑定路径)。而数据源就是通过 DataContext 获得的,并且这个属性的数据可以从父对象继承下来。 下面给 ListBox 指定列表项的数据模板,让它显示的好看一点:

        <ListBox x:Name="list1"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Age}" Margin="20,0" /><TextBlock Text="{Binding Name}" /></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox>

显示如下:

还可以将 DataTemplate 定义到 App 的 Resource 里去,以便于重用。

App.xaml:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightTestApp.App"><Application.Resources><DataTemplate x:Key="ListBoxDataTemplate"><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Age}" Margin="20,0" /><TextBlock Text="{Binding Name}" /></StackPanel></DataTemplate></Application.Resources>
</Application>

Page.xaml:

<UserControl x:Class="SilverlightTestApp.Page"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"><Grid x:Name="LayoutRoot" Background="White"><ListBox x:Name="list1" ItemTemplate="{StaticResource ListBoxDataTemplate}"></ListBox></Grid>
</UserControl>

运行后效果一样。

=====================

接着上一篇,在 Silverlight 中支持3种绑定:OneWay, TwoWay, OneTime. 默认是 OneWay.

其中 OneWay 表示仅仅从数据源绑定到目标(通常是 UI 对象),单向的;

TwoWay 表示既可以从数据源绑定到目标,目标的更改也可以反馈给数据源,使其发生更新。

而 OneTime 是 OneWay 的一种特例,仅加载一次数据。随后数据的变更不会通知绑定目标对象。这样,可以带来更好的性能。

绑定的语法可以用大括号表示,下面是几个例子:

<TextBlock Text="{Binding Age}" />

等同于:

<TextBlock Text="{Binding Path=Age}" />

或者显式写出绑定方向:

<TextBlock Text="{Binding Path=Age, Mode=OneWay}" />

按照数据绑定的语义,默认是 OneWay 的,也就是说如果后台的数据发生变化,前台建立了绑定关系的相关控件也应该发生更新。

比如我们可以将文章 (1) 中提到的数据源改为当前页面的一个私有成员,然后在某个 Button 点击事件中更改其中的值。代码如下:

   public partial class Page : UserControl{private List<Person> persons;public Page(){InitializeComponent();persons = new List<Person>();for(var i=0; i< 5; i++){persons.Add(new Person {Name = "Person " + i.ToString(), Age = 20 + i});}list1.DataContext = persons;}private void Button_Click(object sender, RoutedEventArgs e){persons[0].Name = "Tom";}}

但是我们点击 Button 发现 ListBox 里的数据并没有发生变化。这是因为在数据源更新时,并没有发出任何通知。

我们可以让数据源中的对象实现 INotifyPropertyChanged 接口,在绑定的源属性值发生变化时,发出相关的通知信息。

代码如下:

    public class Person: INotifyPropertyChanged{private int age;public int Age{get{return age;}set{age = value;NotifyPropertyChange("Age");}}private string name;public string Name {get{return name;}set{name = value;NotifyPropertyChange("Name");}}public event PropertyChangedEventHandler PropertyChanged;private void NotifyPropertyChange(string propertyName){if(PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}}}

这个代码的原理很简单,这里就不解释了。这样以后,点击按钮,前台的 ListBox 中第一条数据的人名就变成了 Tom:

俗话说,要知其然,知其所以然。上面的代码说明了在数据源对象中可以设计一个事件通知属性值的变化,并在适当的时候触发之。但是我们并不知道有谁监听了这个事件,并且把这个通知传达到绑定目标对象 (binding target),也就是 UI.

我们用 Reflector 看看 Silverlight 2 beta 2 的源代码,会发现下列两个关键的类:

internal class BindingExpression : BindingExpressionBase
{// ...// 连接到数据源private void ConnectToSource(int index){this._binding._isSealed = true;if (this._binding.Path.PathParts == null){this._cachedValue = this._source;}else if ((this._sourceListeners == null) || (index != this._sourceListeners.Length)){bool flag = false;try{object source;if ((this._sourceListeners == null) && (this._binding.Mode != BindingMode.OneTime)){this._sourceListeners = new WeakPropertyChangedListener[this._binding.Path.PathParts.Length];}if (index == 0){source = this._source;}else{source = this._sourceListeners[--index].Source;}for (int i = index; i < this._binding.Path.PathParts.Length; i++){if (source == null){flag = true;return;}if ((this._binding.Mode != BindingMode.OneTime) && (this._sourceListeners[i] == null)){// 这里尝试创建源对象的属性变更监听器 (A)this._sourceListeners[i] = WeakPropertyChangedListener.CreateIfNecessary(source, this);}this._sourcePropertyInfo = source.GetType().GetProperty(this._binding.Path.PathParts[i]);this._leafSourceObject = source;if (this._sourcePropertyInfo == null){TraceBindingError("The path '" + this._binding._path.Path + "' is invalid");this._leafSourceObject = null;flag = true;return;}try{source = this._sourcePropertyInfo.GetValue(source, null);}catch (TargetInvocationException){TraceBindingError("Could not connect to '" + this._binding._path.Path + "'");this._leafSourceObject = null;return;}}if ((this._binding.Mode == BindingMode.OneTime) || (this._sourceListeners[this._sourceListeners.Length - 1] == null)){this._cachedValue = source;}flag = true;}finally{if (!flag){this.DisconnectFromSource(index);this._sourcePropertyInfo = null;this._leafSourceObject = null;}}}}
}internal class WeakPropertyChangedListener
{// ...// 被 (A) 处代码调用 (B)internal static WeakPropertyChangedListener CreateIfNecessary(object source, BindingExpression bindingExpression){// 查看数据源是否实现了 INotifyPropertyChanged 接口INotifyPropertyChanged notify = source as INotifyPropertyChanged;if (notify != null){// 如果有,创建一个监听器,调用 (C)return new WeakPropertyChangedListener(notify, bindingExpression);}return null;}// 构造函数 (C)private WeakPropertyChangedListener(INotifyPropertyChanged notify, BindingExpression bindingExpression){this._notifyPropertyChanged = notify;// 这里注册事件的回调函数,以便在属性变化时获得通知(调用 D)notify.PropertyChanged += new PropertyChangedEventHandler(this.PropertyChangedCallback);this._weakBindingExpression = new WeakReference(bindingExpression);}// 回调函数 (D)private void PropertyChangedCallback(object sender, PropertyChangedEventArgs args){BindingExpression target = this._weakBindingExpression.Target as BindingExpression;// 这里触发绑定目标对象的更新 (E)if (target != null){target.SourcePropertyChanged(sender, args);}else{this.Disconnect();}}}

由上述代码跟踪可以看到整个调用流程(A -> B -> C -> D):

BindingExpression.ConnectToSource()

-> WeakPropertyChangedListener.ctor()

-> WeakPropertyChangedListener.PropertyChangedCallback()

-> BindingExpression.SourcePropertyChanged()

在 BindingExpression 中连接数据源时,就判断其是否实现了 INotifyPropertyChanged 接口,如果实现了,则注册一个回调函数。

在数据源发生变化时,将触发这个回调函数,在这个函数中调用到 BindingExpression 的 SourcePropertyChanged() 函数去更新目标对象。

这样就实现了一个 source -> target 绑定的数据更新触发机制。

说明:本章出处:http://www.cnblogs.com/RChen/archive/2008/07/04/1235816.html

转载于:https://www.cnblogs.com/zxbzl/archive/2013/03/06/2945878.html

Silverlight 数据绑定 (1):怎样实现数据绑定 Silverlight 数据绑定 (2):Source to Target...相关推荐

  1. Silverlight 3.0 不再包含 asp:silverlight 控件

    从 Silverlight 2 升级到 Silverlight 3 可能会报如下错误,程序中可能引用了 asp:silverlight 控件: Could not load file or assem ...

  2. Silverlight实用窍门系列:51.Silverlight页面控件的放大缩小、Silverlight和Html控件的互相操作...

    本节将讲述三个Silverlight中应用的小技巧:Silverlight页面的放大缩小.Silverlight操作Html.Html操作Silverlight控件. 一.Silverlight页面的 ...

  3. Silverlight三维柱状图3D饼图的Silverlight图表组件案例

    Silverlight三维柱状图3D饼图的Silverlight图表组件案例 开发环境:Visual Studio 2010 Silverlight 4 SQL2005 Silverlight开发的3 ...

  4. 稳扎稳打Silverlight(30) - 2.0Tip/Trick之Silverlight.js, Silverlight.supportedUserAgent.js

    [索引页] [源码下载] 稳扎稳打Silverlight(30) - 2.0Tip/Trick之Silverlight.js, Silverlight.supportedUserAgent.js, 自 ...

  5. Silverlight实用窍门系列:71.Silverlight的Style

    此文章实例基于Silverlight实用窍门系列:68.Silverlight的资源字典ResourceDictionary,如有数据源疑问请参考该文章. 在Silverlight中的Style相当于 ...

  6. Silverlight 4之旅(三)数据绑定(中)

    在上篇文章中我们已经看过了绑定的基础知识,以及绑定数据源的选择问题.在本篇文章中我们看下绑定时Target的现实的问题. 自定义显示 很多时候我们的DataSource存储的数据并不可以直接用来显示, ...

  7. 指令—— 数据绑定指令||数据响应式||双向数据绑定指令

    指令 v-cloak指令的用法 1.提供样式   [v-cloak]{     display: none;   } 2.在插值表达式所在的标签中添加v-cloak指令 背后的原理:先通过样式隐藏内容 ...

  8. 有关Silverlight TreeView组件的研究[3]——Silverlight学习笔记(8)

    三.数据绑定与模板样式 说明:通过学习本文内容,您将了解到怎样动态地进行关于TreeView组件的数据绑定以及TreeViewItem组件的模板样式更改.本文给出基本的使用方法.   注:在Silve ...

  9. 有关Silverlight TreeView组件的研究[2]——Silverlight学习笔记(7)

    二.带复选框的TreeView 说明:在TreeView中设置复选框是十分常见的,这有助于我们对于同组数据的一次性选取或取消.本文就将为你介绍怎样在Silverlight中实现带有Checkbox的T ...

  10. Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地

    在Silverlight中我们可以捕捉视频设备以制作视频会议系统,或者通过视频设备截图功能上传头像等功能. 下面我们通过一个简单的实例来访问视频设备,并且截取图像下载该截图文件至本地. 一.在Silv ...

最新文章

  1. VMware 虚拟化编程(7) — VixDiskLib 虚拟磁盘库详解之三
  2. 无法嵌入互操作类型“……”,请改用适用的接口的解决方法
  3. MySQL 使用Node.js异步查询结果为undefined的简单处理办法
  4. Sentinel的简单使用
  5. 个人计算机预防勒索病毒,Windows10如何开启预防勒索病毒功能|电脑安全开启防御勒索软件的方法...
  6. 关于网络安全检查的问题
  7. 【CSDN软考VIP资料群】让软考通过更容易,软考资料大全支持你软考!
  8. 普通糖尿病人1周食谱
  9. HTL/TTL转光纤模块
  10. 跨境电商APP解决方案
  11. 案牍写作中要留意的问题
  12. 心率检测实现报告(二)
  13. 超搞笑MSN名字大集合(转贴)
  14. 数字逻辑练习题(一)
  15. 谷歌查排名php,百度权重、pagerank、alexa及百度和谷歌收录情况查询接口
  16. 为了更好的实践与应用-机器人与ROS教学与培训
  17. scrapy mysql数据库_Python3学习系列(十三):Scrapy将数据存入Mysql数据库
  18. Nuist 硕士毕业论文 LaTeX 模板
  19. 编解码再进化:Ali266 与下一代视频技术
  20. 关于EXCEL忘记修改权限密码的破解方法

热门文章

  1. 【python】拉格朗日插值法 和 牛顿插值法
  2. GPCP全球月降水量数据下载与读取
  3. ArcGIS学习总结(七)——河流制图综合
  4. 数值分析期末复习(解线性和非线性方程组)
  5. ArcGIS操作小技巧(一)之属性表中显示出小数点前面的 0
  6. oracle 对比 clob,解决比较Oracle中CLOB字段问题
  7. 斐波那契数列。古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子, 假如兔子都不死,问每个月的兔子总数为多少?
  8. 刚刚!中科院主导的国产编程语言木兰负责人回应了!承认32位机器上,是基于Python二次开发!...
  9. 我与潘家园金爷的对话
  10. 插件开发之360 DroidPlugin源码分析(四)Activity预注册占坑