2013-09-27 208 views
2

我想要做這樣的事情:如何綁定到wpf中另一個對象的屬性?

<Label Content="{Binding Path=MyObject.Property}" /> 

而且我想,當MyObject將被分配到另一個ObjectProperty保持不變)

如何做正確它改變?

+0

你的綁定源(I.E這個標籤的'DataContext')必須正確實現'INotifyPropertyChanged'。 –

+0

我的班級會通知「MyObject」(這是一個屬性)的更改是否足夠了?當我更改MyObject時,標籤的內容會自動更新爲「MyObject.Property」嗎? – Adassko

+2

是的。你試過了嗎? –

回答

0

在您的綁定中使用ElementName。

<Label Content="{Binding ElementName=myTextBox, Path=Text}" /> 
0

後續步驟如下:

繼承INotifyPropertyChanged的

public class Data : INotifyPropertyChanged 
    { 
     private int customerID; 

     public int CustomerID 
     { 
      get { return customerID; } 
      set { customerID = value; OnPropertyChanged("CustomerID"); } 
     } 

組的datacontext和出價像下面

<Label Content="{Binding Property, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 

第二種方法: 使用元件結合:

<Label Content="{Binding ElementName=Combo, Path=Text}" Binding path="Property" /> 
相關問題