WPF更新數據源相關操作指南
WPF數據源的操作對于一個初學者來說可能還不能很好的把握其要領。我們在這里就為大家介紹一下有關WPF更新數據源的相關介紹。#t#
WPF更新數據源1.讓對象實現INotifyPropertyChanged接口,以便屬性更改發出通知
- 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更新數據源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}"- />
這里又出現了新的綁定語法,{Binding Path=Age}等價{Binding Age}
WPF更新數據源3.目標:
當更改目標屬性的時候,更新數據源(更新以后則綁定的對象也發生變化,如更改TextBox的Text則Label的Content也發生變化)
WPF更新數據源4.設置更新數據源執行時間
通過設置Binding對象的UpdateSourceTrigger 來確定執行時間.
根據需要設置UpdateSourceTrigger 屬性