1.让对象实现INotifyPropertyChanged接口,以便属性更改发出通知
publicPerson(){}
publicPerson(stringname,intage)
{
this.name=name;
this.age=age;
}
stringname;
publicstringName
{
get{returnthis.name;}
set
{
this.name=value;
OnPropertyChanged("Name");
}
}
intage;
publicintAge
{
get{returnthis.age;}
set
{
this.age=value;
OnPropertyChanged("Age");
}
}
publiceventPropertyChangedEventHandlerPropertyChanged;
protectedvoidOnPropertyChanged(stringpropName)
{
if(this.PropertyChanged!=null)
{
PropertyChanged(this,newPropertyChangedEventArgs(propName));
}
}
}
2.xaml(略去布局)
<LabelContent="{BindingName}"></Label>
<LabelContent="{BindingAge}"></Label>
<TextBoxText="{BindingPath=Name,Source={StaticResourceTom}}"/>
<TextBoxText="{BindingAge}"
/>
3.目标:
当更改目标属性的时候,更新数据源(更新以后则绑定的对象也发生变化,如更改TextBox的Text则Label的Content也发生变化)
4.设置更新数据源执行时间
通过设置Binding对象的UpdateSourceTrigger 来确定执行时间.
根据需要设置UpdateSourceTrigger属性
完