
oneTime:一次性绑定,将数据给控件,绑定就结束
oneWay:数据源改变会影响绑定该数据源的控件
twoWay:数据源改变会影响绑定该数据源的控件,并且控件中数据改变时也会影响到数据源
一、oneTime
前台:
<TextBox Text="{Binding name,Mode=OneTime}" Height="23" HorizontalAlignment="left" margin="114,92,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
前台:
class Person : INotifyPropertyChanged//要实现oneWay和twoWay的绑定需要实现一个接口 { string name; string name { get { return name; } set { name = value; NotifyChange(name"); } } #region INotifyPropertyChanged 成员 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; } voID button1_Click(object sender,RoutedEventArgs e) { p.name = 段誉"; }
当点击oneWay这个按钮时,数据源中的改变将会影响绑定了该数据源的控件。
三、twoWay
前台:
让textBox1和textBox2都绑定相同的数据源,前者以oneWay模式绑定,后者以twoWay模式绑定。当修改了textBox2文本框中的内容并让文本框失去焦点时,数据源的值也改变,使textBox1和textBox2都显示新的值。
转自:http://www.cnblogs.com/sydeveloper/archive/2012/04/27/2443098.HTML
总结以上是内存溢出为你收集整理的Silverlight/WPF数据绑定oneTime,oneWay,twoWay全部内容,希望文章能够帮你解决Silverlight/WPF数据绑定oneTime,oneWay,twoWay所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)