惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
T
Tailwind CSS Blog
小众软件
小众软件
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
博客园_首页
MyScale Blog
MyScale Blog
博客园 - 聂微东
V
Visual Studio Blog
The Cloudflare Blog
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42

博客园 - hades

python + selenium webdriver 自动化测试 之 环境异常处理 (持续更新) 推荐一个娱乐化学习python的网站 Silverlight 导出Excel Silverlight Control(三)DataGrid Silverlight Control(二)ListBox Silverlight 中使用 WCF RIA Service Silverlight Control(六)GroupBox Silverlight Control(五)TimePicker 在Silverlight中请谨慎使用MVVM Silverlight Chart 综合运用(样式、多轴、数据绑定、点状图、线形图、DataGrid、Chart导出综合运用) C# 关闭其他程序窗口、进程 Silverlight Control(四)Chart (1) 初体验 Silverlight 如何导出图片 Silverlight Control(一)ComboBox 数据绑定、自定义、取值 Silverlight Style (二) 自定义样式在后台代码中应用 Silverlight Style (一) 如何在页面应用样式 ASP.NET项目中不能有重名的文件夹 给部队做项目开发的一点想法 企业工作流的定制(SPS实现)
Silverlight Binding (One Time,One Way,Two Way)
hades · 2010-08-05 · via 博客园 - hades

  本文将探讨Silverlight绑定的三种方式-One time,One way,Two way。

  One way binding

  从名称就可以了解到该方法只支持单向的从数据对象到UI的绑定。例如,界面上有一个textbox name='txtYear',text属性绑定了某个数据对象的'year'属性。

则一旦该数据对象发生了改变,txtYear的text会跟随改变。但是相反的,txtYear的text属性的改变却不会影响到数据对象。

<TextBox x:Name="txtYear" Text="{Binding Year, Mode=OneWay}"></TextBox>

下面是绑定的方法。

 private void bind()
{   
DateClass myDate
= new DateClass ();
myDate .Year
= DateTime.Now.Year;   
txtYear.DataContext
= myDate ;
}

  Two way binding

  Two way binding模式下,数据在数据对象和UI之间是同步的,一旦任何一方发生了改变,都会同时改变另一方。

<TextBox x:Name="txtYear" Text="{Binding Year, Mode=TwoWay}" ></TextBox>

  需要注意的是,如果是自定义的数据对象,我们需要引用System.ComponentModel,继承INotifyPropertyChanged接口

并在该对象中实现PropertyChanged。

代码

public class DateClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _intYear;
public int Year{
set
{  
_intYear
= value;  
OnPropertyChanged(
"Year");
}
get
{  
return _intYear;
}
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{  
PropertyChanged(
this,new PropertyChangedEventArgs(property));
}
}
}

   与UI的绑定方法同上。

  One time binding

  One time binding模式下,数据仅仅与UI绑定一次,数据对象与UI任何一方发生改变都不会影响到另一方面,One time binding在效率上比前两种高,比较适用于报表之类只需要绑定一次数据的UI对象。

<TextBox x:Name="txtYear" Text="{Binding Year, Mode=OneTime}" ></TextBox>

  如上所述,Silverlight的三种绑定方式就是如此,至于什么时候用哪种方式就取决当时的情况了。