WPF更新數(shù)據(jù)源相關(guān)操作指南
WPF數(shù)據(jù)源的操作對(duì)于一個(gè)初學(xué)者來說可能還不能很好的把握其要領(lǐng)。我們?cè)谶@里就為大家介紹一下有關(guān)WPF更新數(shù)據(jù)源的相關(guān)介紹。#t#
WPF更新數(shù)據(jù)源1.讓對(duì)象實(shí)現(xiàn)INotifyPropertyChanged接口,以便屬性更改發(fā)出通知
- public class Person :
INotifyPropertyChanged - {
- public Person() { }
- public Person(string
name, int age) - {
- this.name = name;
- this.age = age;
- }
- string name;
- public string Name
- {
- get { return this.name; }
- set
- {
- this.name = value;
- OnPropertyChanged("Name");
- }
- }
- int age;
- public int Age
- {
- get { return this.age; }
- set
- {
- this.age = value;
- OnPropertyChanged("Age");
- }
- }
- public event PropertyChanged
EventHandler PropertyChanged; - protected void OnProperty
Changed(string propName) - {
- if (this.PropertyChanged != null)
- {
- PropertyChanged(this, new
PropertyChangedEventArgs(propName)); - }
- }
- }
WPF更新數(shù)據(jù)源2.xaml(略去布局)
- < Label Content=
"{Binding Name}">< /Label>- < Label Content=
"{Binding Age}">< /Label>- < TextBox Text="{Binding
Path=Name, Source={Static
Resource Tom}}" />- < TextBox Text="
{Binding Age}"- />
這里又出現(xiàn)了新的綁定語法,{Binding Path=Age}等價(jià){Binding Age}
WPF更新數(shù)據(jù)源3.目標(biāo):
當(dāng)更改目標(biāo)屬性的時(shí)候,更新數(shù)據(jù)源(更新以后則綁定的對(duì)象也發(fā)生變化,如更改TextBox的Text則Label的Content也發(fā)生變化)
WPF更新數(shù)據(jù)源4.設(shè)置更新數(shù)據(jù)源執(zhí)行時(shí)間
通過設(shè)置Binding對(duì)象的UpdateSourceTrigger 來確定執(zhí)行時(shí)間.
根據(jù)需要設(shè)置UpdateSourceTrigger 屬性