【问题标题】:IDataErrorInfo not working when binding to child绑定到孩子时,IDataErrorInfo 不起作用
【发布时间】:2017-10-10 09:14:17
【问题描述】:

我有两个 xaml 代码行。

<TextBox Text="{Binding Path=MyEntity.Name, ValidatesOnDataErrors=True}" />

<ComboBox ItemsSource="{Binding EntityCollection}" 
          SelectedItem="{Binding Path=MyEntity.ChildEntity.NestedChildEntity}" 
          SelectedValue="{Binding Path=MyEntity.ChildEntity.ChildProperty, ValidatesOnDataErrors=True}"
          SelectedValuePath="PK"/>

我所有的实体都通过一个基类 IDataErrorInfo 和他的索引器来实现。

MyEntityChildEntityNestedChildEntity 是数据库实体。 MyEntity 具有到 ChildEntity 的导航属性。 ChildEntity 具有到 NestedChildEntity 的导航属性。 ChildProperty 在 ChildEntity 中是必需的。 ChildProperty 是外键,NestedChildEntity 是实体。如果我不在组合框中选择一个值,ChildProperty 将为空,通常它不能为空。

EntityCollectionList

类型

MyEntity.cs

public class MyEntity : BaseEntityClass
{
    [Key]
    [Required]
    public long PK { get; set; }
    public string Name { get; set; }
    public ChildEntity ChildEntity { get; set; }
}

ChildEntity.cs

public class ChildEntity : BaseEntityClass
{
    [Key]
    [Required]
    public long PK { get; set; }

    [Required]
    public long ChildProperty { get; set; }

    [ForeignKey("ChildProperty")]
    public NestedChildEntity NestedChildEntity { get; set; }
}

NestedChildEntity.cs

public class NestedChildEntity : BaseEntityClass
{
    [Key]
    [Required]
    public long PK { get; set; } 
}

BaseEntityClass.cs

public class BaseEntityClass : IDataErrorInfo
{
    public string Error
    {
        get
        {
            return null;
        }
    }

    public string this[string propertyName]
    {
        get
        {
            //Check the Required and StringLength attribute 
            var annotationValidationError = GetAnnotationValidationError(propertyName);

            if (annotationValidationError == null)
            {

                return GetValidationError(propertyName);
            }
            else
            {
                return annotationValidationError;
            }
        }
    }
}

对于第一行,验证有效,到达我的基类中的索引器,并将属性名称作为参数发送(在本例中为“名称”)

对于第二行,验证永远不会到达。即使我的 ChildEntity 类实现(通过基类)IDataErrorInfo。

为什么嵌套绑定会有这样的行为?我该如何解决?

【问题讨论】:

    标签: c# wpf validation data-binding


    【解决方案1】:

    为什么要同时绑定SelectedItemSelectedValueSelectedValue 属性永远不会被设置。

    只要NestedChildEntity 属性返回的对象实现了IDataErrorInfo 接口并且该对象的类型与EntityCollection 中的项目类型匹配,这应该可以工作:

    <ComboBox ItemsSource="{Binding EntityCollection}" 
          SelectedItem="{Binding Path=MyEntity.ChildEntity.NestedChildEntity, ValidatesOnDataErrors=True}" />
    

    【讨论】:

    • 我更新了我的问题以便更好地理解。我使用 SelectedValue 是因为我也使用 SelectedValuePath。我尝试按照您的建议将 ValidatesOnDataErrors 放在 SelectedItem 上,但它什么也没做
    • 那你为什么还要使用 SelectedItem?您的房产类型是什么?
    • 老实说,我知道这有点多余,但它是遗留代码。编码人员这样做是为了确保 EntityFramework 不会在仅设置其中一个属性时抱怨。 EntityCollection = 列表。它们都是 POCO,除了 ChildPropertylong
    • 我改进了问题
    • ChildEntity 似乎没有实现 IDataErrorInfo?
    【解决方案2】:

    好的,我终于找到了解决方案。

    之前,在我的视图模型构造函数中,我像这样初始化了 MyEntity 属性:

    MyEntity = new MyEntity ();
    

    问题是我的 {Binding Path=MyEntity.ChildEntity.ChildProperty} , MyEntity 中的 ChildEntity 未初始化。

    我把构造函数改成这样:

    MyEntity = new MyEntity { ChildEntity = new ChildEntity() };
    

    现在一切正常!

    感谢您的帮助@mm8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-15
      • 2015-12-14
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多