2013-09-27 207 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" /> 
相关问题