2011-06-23 31 views
2

我有一个对象MyPerson与属性FirstNameLastName,和FullName其中从数据源刷新GUI在Silverlight

public string FullName 
{ 
    get {return LastName + " " + FirstName;} 
    set {...} 
} 

我的MyPerson绑定到UserControl,其中我结合名字,姓氏和FULLNAME到3个texboxes。

现在,当我修改FirstName或LastName时,我需要向UserControl指示“更新”FullName。

什么应该是这个“更新”命令?

的Silverlight 4

回答

3

你或许应该看看到INotifyPropertyChanged接口。这会让你的生活更轻松。


实施例:

public class Person : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
    } 

    private String _FirstName; 
    private String _LastName; 

    public String FirstName 
    { 
    get 
    { 
     return this._FirstName; 
    } 
    set 
    { 
     if (this._FirstName != value) 
     { 
     this._FirstName = value; 
     this.NotifyPropertyChanged("FirstName"); 
     this.NotifyPropertyChanged("FullName"); 
     } 
    } 
    } 

    public String LastName 
    { 
    get 
    { 
     return this._LastName; 
    } 
    set 
    { 
     if (this._LastName != value) 
     { 
     this._LastName = value; 
     this.NotifyPropertyChanged("LastName"); 
     this.NotifyPropertyChanged("FullName"); 
     } 
    } 
    } 

    public String FullName 
    { 
    get 
    { 
     return String.Format("{0} {1}", this.LastName, this.FirstName); 
    } 
    } 
} 
+0

我试图实现它。通知名字,通知姓氏...但这似乎并没有wrok – serhio

+0

@serhio:看到我更新的答案。 –

+0

现在,我看到我应该通知不仅修改后的属性,而且还通知属性....这有点尴尬......如果我们不知道先验所有依赖属性......但这是一个解决方案然而... – serhio