黑马程序员_数据绑定,INotifyPropertyChanged接口,DataContext
来源:互联网 发布:那家博客公司数据准确 编辑:程序博客网 时间:2024/05/21 06:12
------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------
1、两个控件之间的数据绑定
<Slider Name="slider1" Margin="0,40" Width="300" VerticalAlignment="Top" HorizontalAlignment="Center" ></Slider>
<TextBox Text="{Binding Value,ElementName=slider1}" Width="300" Height="30" HorizontalAlignment="Center"></TextBox>
2、数据绑定基础:
1)定义一个类:Person
2) new一个实例,让控件的DataContext引用对象:
Person p1 = new Person();
txtName.DataContext = p1
3)将控件的属性绑定到对象的属性,几乎所有的属性都可以这样绑定
<TextBox Name="txtName" Text="{Binding Name}" Width="300" Height="30" HorizontalAlignment="Center"></TextBox>
3、INotifyPropertyChanged 接口
数据绑定会检测DataContext是否实现INotifyPropertyChanged 接口,如果实现,通过监听PropertyChanged通知属性改变。实现前台页面随后台对象属性的改变而改变。
例:public class Person:INotifyPropertyChanged
{
private int age;
public int Age
{
get { return age; }
set
{
this.age = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
}
private string name;
public string Name
{
get { return name; }
set
{
this.name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
4、所有子控件及其后代控件在没有指定DataContext的情况下都默认继承自其父控件的DataContext。
例:
//前台
<Grid Name="grid1">
<TextBox Name="txtAge" Text="{Binding Age}" ToolTip="{Binding Name}" Width="200" Height="30" Margin="59,164,258,125"></TextBox>
<TextBox Name="txtName" Text="{Binding Name}" ToolTip="{Binding Age}" Width="200" Height="30" Margin="59,210,258,79"></TextBox>
</Grid>
//后台绑定
grid1.DataContext = p1;
txtName.DataContext = p2;
- 黑马程序员_数据绑定,INotifyPropertyChanged接口,DataContext
- 【黑马程序员】数据绑定的INotifyPropertyChanged接口:监听和同步
- InotifyPropertyChanged接口实现简单数据绑定
- Silverlight 实现INotifyPropertyChanged接口绑定数据
- 黑马程序员_接口
- 黑马程序员_接口
- 黑马程序员_接口
- 黑马程序员_接口
- C#数据绑定之INotifyPropertyChanged
- 黑马程序员数据绑定
- 黑马程序员---数据绑定
- 绑定数据,指定数据源DataContext
- 黑马程序员_接口整理
- 黑马程序员_类、接口
- 黑马程序员_细说接口
- 黑马程序员_接口interface
- SilverLight 学习笔记--Silverlight中INotifyPropertyChanged 接口在数据绑定中的使用
- WPF学习笔记:(二)数据绑定模式与INotifyPropertyChanged接口
- C++即时通讯实现原理及常见问题
- java经典面试题
- c# 设置窗体始终最前端
- 如何优化手机网站
- 2012 macbook pro md101 安装固态硬盘和内存 总结
- 黑马程序员_数据绑定,INotifyPropertyChanged接口,DataContext
- 黑马程序员_ADO.NET连接数据库
- c# 悬浮窗口
- 获取数据窗口数据列的数据类型
- hdu 2117 (模拟除法运算)
- 25个很酷的jQuery倒计时脚本–添加动态计数器!
- JAVA中this用法小结
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException||java.net.SocketException: Broken pipe
- jQuery 2.0发布 不再支持IE 6/7/8