【问题标题】:WPF Binding : Object in a objectWPF绑定:对象中的对象
【发布时间】:2011-02-04 14:58:53
【问题描述】:

我在 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 属性没有绑定。我做错了什么?

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    您可能没有在类中实现 INotifyPropertyChanged,并且可能在绑定后分配值。如果您尝试 Snoop http://snoopwpf.codeplex.com/,您可以找出确切的问题。

    【讨论】:

    • 我的 ContactEntity 中缺少 INotifyPropertyChanged。它在我的 ViewModel 中,但不在我的实体中。非常感谢!
    【解决方案2】:

    一切看起来都很好,在发生绑定时检查 Address 属性是否不为空。您也可以在调试时检查 Visual Studio 输出窗口,看看是否有任何绑定错误。

    【讨论】:

      【解决方案3】:

      有一些很好的资源可用于调试绑定here。详细的转换器方法我用过很成功。

      【讨论】:

        猜你喜欢
        • 2011-04-24
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多