如何绑定到wpf中另一个对象的属性?
问题描述:
我想要做这样的事情:如何绑定到wpf中另一个对象的属性?
<Label Content="{Binding Path=MyObject.Property}" />
而且我想,当MyObject
将被分配到另一个Object
(Property
保持不变)
如何做正确它改变?
答
在您的绑定中使用ElementName。
<Label Content="{Binding ElementName=myTextBox, Path=Text}" />
答
后续步骤如下:
继承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" />
你的绑定源(I.E这个标签的'DataContext')必须正确实现'INotifyPropertyChanged'。 –
我的班级会通知“MyObject”(这是一个属性)的更改是否足够了?当我更改MyObject时,标签的内容会自动更新为“MyObject.Property”吗? – Adassko
是的。你试过了吗? –