WPF 绑定:对象中的对象
我在 WPF 中有一个带有 2 个文本框的表单:
<TextBox Name="txtName" Text="{Binding Contact.Name}"/>
<TextBox Name="txtAddressNumber" Text="{Binding Contact.Address.Number}"/>
并且我有 2 个类:
public class ContactEntity
{
public string Name {get;set;}
public AddressEntity Address {get;set;}
}
public class AddressEntity
{
public int Number {get;set}
}
Name 属性绑定得很好。但是 Contact 对象内的 Address 对象的 Number 属性并未绑定。我做错了什么?
I have a form in WPF with 2 textbox :
<TextBox Name="txtName" Text="{Binding Contact.Name}"/>
<TextBox Name="txtAddressNumber" Text="{Binding Contact.Address.Number}"/>
and I have 2 class :
public class ContactEntity
{
public string Name {get;set;}
public AddressEntity Address {get;set;}
}
public class AddressEntity
{
public int Number {get;set}
}
The Name property binds fine. But the Number property of the Address object inside the Contact object does not binds. What I'm doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能没有在类中实现 INotifyPropertyChanged,并且可能在绑定后分配值。如果您尝试 Snoop http://snoopwpf.codeplex.com/ 您可以找出确切的问题。
You may not be implementing INotifyPropertyChanged in the classes and may be assigning the value after the binding. If you try Snoop http://snoopwpf.codeplex.com/ you can find out the exact problem.
一切看起来都很好,请检查绑定发生时 Address 属性是否不为 null。您还可以在调试时检查 Visual Studio 输出窗口,以查看是否存在任何绑定错误。
Everything looks fine, check that the Address property is not null when binding occurs. Also you can check Visual Studio output window while under debug to see whether there are any binding errors.
此处提供了一些用于调试绑定的好资源。我已经非常成功地使用了详细的转换器方法。
There's a few good resources for debugging bindings here. I've used the converter method detailed very successfully.