5-1.Binding的方向、数据更新和Path
Binding可以看做是数据源和目标对象之间的桥梁,源为逻辑层的对象,目标对象则是UI层的控件对象。
基础
创建数据源对象类
class Student
{
private string name;
public string Name
{
get {
return name;}
set {
name = value;}
}
}
如果想让Name属性通过Binging送达到UI上,则要设置Binging的Path为Nmae。
不光如此,如果属性值发生变化后,属性要有能力通知Binging,可以在属性的set方法中出发一个PropertyChanged事件,即继承INotifyPropertyChanged接口。当数据源变化后,Binding会自动监听来自这个接口的PropertyChanged事件。
public class Student:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get {
return name; }
set
{
name = value;
if (PropertyChanged!=null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
案例:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

最低0.47元/天 解锁文章
1304

被折叠的 条评论
为什么被折叠?



