我正在更新一些现有的 WPF 代码,我的应用程序有许多定义如下的文本块:
<TextBlock x:Name="textBlockPropertyA"><Run Text="{Binding PropertyA}"/></TextBlock>
在本例中,“PropertyA”是我的业务类对象的属性,定义如下:
public class MyBusinessObject : INotifyPropertyChanged
{
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private string _propertyA;
public string PropertyA
{
get { return _propertyA; }
set
{
if (_propertyA == value)
{
return;
}
_propertyA = value;
OnPropertyChanged(new PropertyChangedEventArgs("PropertyA"));
}
}
// my business object also contains another object like this
public SomeOtherObject ObjectA = new SomeOtherObject();
public MyBusinessObject()
{
// constructor
}
}
现在我有一个 TextBlock,需要将其绑定到 ObjectA 的属性之一,如您所见,它是 MyBusinessObject 中的一个对象。在代码中,我将其称为:
MyBusinessObject.ObjectA.PropertyNameHere
与我的其他绑定不同,“PropertyNameHere”不是 MyBusinessObject 的直接属性,而是 ObjectA 上的属性。我不确定如何在 XAML 文本块绑定中引用它。谁能告诉我该怎么做?谢谢!
Before <Run Text="{Binding ObjectA.PropertyNameHere}" />
你必须做的工作ObjectA
本身是一个属性,因为绑定仅适用于属性而不适用于字段。
// my business object also contains another object like this
public SomeOtherObject ObjectA { get; set; }
public MyBusinessObject()
{
// constructor
ObjectA = new SomeOtherObject();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)