自定义数据注释不显示错误消息

时间:2012-10-19 20:14:46

标签: c# xaml mvvm data-annotations

我正在制作自定义数据注释,我正在做一些测试,我无法弄清楚为什么它不显示我的错误信息。

PersonInfo.cs

[CustomValidation(typeof(AWValidation), "ValidateName")]
public String GivenName { get; set; }

AWValidation.cs

    public static ValidationResult ValidateName(String name)
    {
        // Perform validation logic here and set isValid to true or false.

        if (name != null)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult(
                "The name for this customer does not match the required criteria.");
        }
    }

仅当用户单击“保存”按钮时才输入ValidationResult ValidateName(String name),虽然它不显示ValidationResult,但会显示其默认错误消息。

我想要的是当用户选中或丢失GivenName文本框的焦点以显示我的自定义数据注释时。

我的同事告诉我这可能是因为它验证了服务器端GivenName,我需要让它验证客户端吗?虽然我不明白为什么

[Required(ErrorMessage="This is a required value")]
public String GivenName { get; set; }
当我从GivenName文本框中丢失焦点/标签时,

显示。

任何输入都将非常感谢! 谢谢, 杰森:) (P.S我正在使用MVVM)

0 个答案:

没有答案
相关问题