Silverlight/WPF数据绑定oneTime,oneWay,twoWay
2016-04-27 10:03
537 查看
oneTime:一次性绑定,将数据给控件,绑定就结束
oneWay:数据源改变会影响绑定该数据源的控件
twoWay:数据源改变会影响绑定该数据源的控件,并且控件中数据改变时也会影响到数据源
一、oneTime
前台:
后台:
二、oneWay
前台:
后台:
当点击oneWay这个按钮时,数据源中的改变将会影响绑定了该数据源的控件。
三、twoWay
前台:
后台:
让textBox1和textBox2都绑定相同的数据源,前者以oneWay模式绑定,后者以twoWay模式绑定。当修改了textBox2文本框中的内容并让文本框失去焦点时,数据源的值也改变,使textBox1和textBox2都显示新的值。
转自:http://www.cnblogs.com/sydeveloper/archive/2012/04/27/2443098.html
oneWay:数据源改变会影响绑定该数据源的控件
twoWay:数据源改变会影响绑定该数据源的控件,并且控件中数据改变时也会影响到数据源
一、oneTime
前台:
<TextBox Text="{Binding Name,Mode=OneTime}" Height="23" HorizontalAlignment="Left" Margin="114,92,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
后台:
public class Person { string name; public string Name { get { return name; } set { name = value; } } } Person p = new Person(); public MainPage() { InitializeComponent(); p.Name = "乔峰"; textBox1.DataContext = p; }
二、oneWay
前台:
<TextBox Text="{Binding Name,Mode=OneWay}" Height="23" HorizontalAlignment="Left" Margin="114,92,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /> <Button Content="oneWay" Height="23" HorizontalAlignment="Left" Margin="159,182,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
后台:
public class Person : INotifyPropertyChanged//要实现oneWay和twoWay的绑定需要实现一个接口 { string name; public string Name { get { return name; } set { name = value; NotifyChange("Name"); } } #region INotifyPropertyChanged 成员 public event PropertyChangedEventHandler PropertyChanged; private void NotifyChange(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } Person p = new Person(); public MainPage() { InitializeComponent(); p.Name = "乔峰"; textBox1.DataContext = p; } private void button1_Click(object sender, RoutedEventArgs e) { p.Name = "段誉"; }
当点击oneWay这个按钮时,数据源中的改变将会影响绑定了该数据源的控件。
三、twoWay
前台:
<TextBox Text="{Binding Name,Mode=OneWay}" Height="23" HorizontalAlignment="Left" Margin="114,92,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /> <TextBox Text="{Binding Name,Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="114,144,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
后台:
public class Person : INotifyPropertyChanged//要实现oneWay和twoWay的绑定需要实现一个接口 { string name; public string Name { get { return name; } set { name = value; NotifyChange("Name"); } } #region INotifyPropertyChanged 成员 public event PropertyChangedEventHandler PropertyChanged; private void NotifyChange(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } Person p = new Person(); public MainPage() { InitializeComponent(); p.Name = "乔峰"; textBox1.DataContext = p; textBox2.DataContext = p; }
让textBox1和textBox2都绑定相同的数据源,前者以oneWay模式绑定,后者以twoWay模式绑定。当修改了textBox2文本框中的内容并让文本框失去焦点时,数据源的值也改变,使textBox1和textBox2都显示新的值。
转自:http://www.cnblogs.com/sydeveloper/archive/2012/04/27/2443098.html
相关文章推荐
- 函数自执行
- java继承关系中成员变量,构造方法,成员方法的关系
- PSP(4.20——4.26)以及周记录
- K-Means聚类算法的原理及实现【转】
- 简介BootStrap model弹出框的使用
- 第十一章 属性设置或返回元素的class 属性
- 检测浏览器是否安装flash
- mysql load data infile的使用
- 把只有框架的立体图添加面
- Oracle 包(Package)
- Android 上传图片到后台的一直方式Base64的String形式
- 我这样理解技术人的成长过程
- Android res目录下的color.xml颜色总结
- iOS 通知传值
- Android开发实践:使用Service还是Thread
- 插入数据库中文乱码问题
- windows上更好的命令行工具软件
- 第十章 显示和隐藏(display属性)
- Referenced file contains errors (http://struts.apache.org/dtds/struts-2.0.dtd)
- HTTP无状态协议和Connection:Keep-Alive容易犯的误区