如何显示DataAnnotations的错误消息

时间:2015-06-02 02:26:30

标签: c# entity-framework mvvm data-annotations ef-database-first

我花了最后一小时左右的时间用谷歌搜索答案,但几乎每一个结果都是在ASP.NET或者谈论Code First方法,这对我来说是无用的。

我基本上得到了数据库优先的实体框架POCO对象,我提供了使用IDataErrorInfo的验证。

现在这个工作正常,除了我有一个200行长的索引器,里面有大约40个if语句。 (我很认真。)

我现在正在做的是像这样扩展课程:

public partial class MyPocoObject : IDataErrorInfo
{
    public string Error
    {
        get { throw new NotImplementedException("IDataErrorInfo.Error"); }
    }

    public string this[string columnName]
    {
        get
        {
            string result = null;

            // There's about 40 if statements here...
        }
    }
}

显然,这是错误的,所以我尝试使用DataAnnotations。

这是我迄今所理解的。

我已经创建了元数据类,如下所示:

[MetadataType(typeof(MyObjectMetaData))]
public partial class MyObject { }

public class MyObjectMetaData
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "Forename is a required field.")]
    public string Forename;
}

然后我将控件声明为:

<TextBox Text="{Binding SelectedObject.Forename, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>

然后我在其他地方有一个触发器:

<Style TargetType="TextBox" BasedOn="{StaticResource Global}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

当我使用IDataErrorInfo执行此操作时,验证失败时,我会得到一个带有错误消息的红色边框和工具提示。使用数据注释我什么都没得到。

我应该如何实施?因为单个方法中的40个if语句是疯狂的。

更新 我已经尝试了答案中建议的代码,虽然我认为我做错了。

public partial class Member : IDataErrorInfo
{
    // Error: The type 'Project.Client.Models.Member' already contains a definition for 'Forename'
    [Required(AllowEmptyStrings = false, ErrorMessage = "Forename is a required field.")]
    public string Forename;

    private readonly Dictionary<string, object> _values = new Dictionary<string, object>();

    public string Error
    {
        get { throw new NotImplementedException("IDataErrorInfo.Error"); }
    }

    public string this[string columnName]
    {
        get { return OnValidate(columnName); }
    }


    protected virtual string OnValidate(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
        {
            throw new ArgumentException("Invalid property name", propertyName);
        }

        string error = string.Empty;
        // Error: Project.Client.Models.Member.GetValue<T>(string)' cannot be inferred from the usage. Try specifying the type arguments explicitly
        var value = GetValue(propertyName);
        var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>(1);
        var result = Validator.TryValidateProperty(
            value,
            new ValidationContext(this, null, null)
            {
                MemberName = propertyName
            },
            results);

        if (!result)
        {
            var validationResult = results.First();
            error = validationResult.ErrorMessage;
        }

        return error;
    }

    protected T GetValue<T>(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
        {
            throw new ArgumentException("Invalid property name", propertyName);
        }

        object value;
        if (!_values.TryGetValue(propertyName, out value))
        {
            value = default(T);
            _values.Add(propertyName, value);
        }

        return (T)value;
    }
}

更新2: MVVM上的文章中链接的代码似乎有效,但即使OnValidate正在触发,也不会显示任何错误消息。

虽然现在还有其他错误...即使文本框的内容发生了变化,GetValue返回的值也始终相同,而且对于未从我的数据库加载的空对象,验证不会触发一点都不。

3 个答案:

答案 0 :(得分:1)

您似乎必须手动集成DataAnnotations验证。也许System.ComponentModel.DataAnnotations.Validator默认情况下不使用MetadataType,但在TypeDescriptor中注册它就像我以前的答案一样。

Article Code

我的意思是你必须实现你的验证方法并使用System.ComponentModel.DataAnnotations.Validator。

在我链接的源代码的PropertyChangedNotification实现中获取战利品:

nonatomic

答案 1 :(得分:0)

某些.NET技术检索并访问MetadataTypes,而其他.NET技术则不会。 尝试在使用前注册好友类:

var myObjectMetadaProviderProvider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(MyObject), typeof(MyObjectMetaData));
TypeDescriptor.AddProvider(myObjectMetadaProviderProvider , typeof(MyObject));

这允许以透明的方式检索在伙伴类中声明的属性,而无需了解主类中的MetadataTypes。

答案 2 :(得分:0)

我最终通过绑定到viewmodel中的属性而不是模型本身并将注释添加到视图模型而不是尝试扩展和混乱模型来自行修复此问题。

考虑一下,我可能应该先做到这一点......