2012-10-15 134 views
0

我有一個叫VoucherEntity類,包括一個名爲「客戶」,類CustomerEntity的對象屬性,所以我有婁代碼,WPF文本框綁定

<TextBox Height="23" IsReadOnly="False" HorizontalAlignment="Stretch" Margin="124,48,174,0" Name="txt_customer" VerticalAlignment="Top" Text="{Binding Path=Customer.Name}" /> 
在cs文件

,我有婁代碼

_voucher = new VoucherEntity(); 
this.DataContext = _voucher; 

這意味着,在第一,顧客屬性爲null,之後點擊一個按鈕,我會給_voucher一個CustomerEntity對象的顧客財產,那麼我希望在文本框可以立即顯示出來,但失敗了,我應該怎麼做?

回答

0

如果您想要除視圖中的更改外,還應通知有關更改的視圖。

所以才實現了VoucherEntityINotifyPropertyChanged界面後,你設置的客戶支撐

public class VoucherEntity: INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void FirePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    private CustomerEntity _customer; 
    public CustomerEntity Customer 
    { 
     get {return _customer;} 
     set 
     { 
      if (_customer != value) 
      { 
       _customer= value; 
       FirePropertyChanged("Customer"); 
      } 
     } 
    } 
} 
+0

是的,這是解決我的問題的方式觸發PropertyChanged事件,但我並不想這樣做,因爲它會污染我的類,只有WPF需要這些接口,我有另一種方式,txt_customer.GetBindingExpression(TextBox.TextProperty).UpdateSource();它也可以工作,但我需要將CustomerEntity對象給予VoucherEntity對象在點擊按鈕選擇客戶之前。 –