Silverlight数据绑定oneTime,oneWay,twoWay
2012-04-27 22:42
246 查看
oneTime:一次性绑定,将数据给控件,绑定就结束
oneWay:数据源改变会影响绑定该数据源的控件
twoWay:数据源改变会影响绑定该数据源的控件,并且控件中数据改变时也会影响到数据源
一、oneTime
前台:
后台:
二、oneWay
前台:
后台:
当点击oneWay这个按钮时,数据源中的改变将会影响绑定了该数据源的控件。
三、twoWay
前台:
后台:
让textBox1和textBox2都绑定相同的数据源,前者以oneWay模式绑定,后者以twoWay模式绑定。当修改了textBox2文本框中的内容并让文本框失去焦点时,数据源的值也改变,使textBox1和textBox2都显示新的值。
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都显示新的值。
相关文章推荐
- silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
- silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
- Silverlight/WPF数据绑定oneTime,oneWay,twoWay
- silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
- silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
- silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
- SL4.数据绑定--OneWay、OneTime、TwoWay
- wpf数据绑定的三种模式:oneTime、OneWay、.TwoWay
- Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解 .
- Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解
- Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解
- 欢迎捧场:WPMind WP7 开发技术在线学习【5】-- Silverlight数据绑定
- 关于Silverlight对匿名类型数据绑定的问题及其解决方法
- silverlight中使用Blend3绑定数据学习
- Silverlight 3 系列(四)数据绑定 Data Binding
- 在 Silverlight 中使用 IValueConverter 实现对绑定数据的格式化
- SilverLight中的数据绑定
- silverlight绑定弱类型数据
- Silverlight中为ComboBox设定当前选择项、数据绑定、取值。
- Silverlight中使用MVVM(9)-绑定Enum类型数据