【问题标题】:WPF: Validation with IDataErrorInfo and low reuseability with similar validation rulesWPF:使用 IDataErrorInfo 进行验证,并且具有类似验证规则的低可重用性
【发布时间】:2010-05-28 20:52:12
【问题描述】:

我有一个实现 IDataErrorInfo 的学生实体:

现在,我想针对姓氏、性别、街道、城市、邮政和电话验证完全相同的规则。

我真的必须重复所有这些吗?使用 ValidationRule 类会更好,但我

无法通过 ICommand 处理禁用/启用按钮。

...

 #region Validation Rules

    private string ValidateFirstName()
    {
        if (IsStringMissing(this.FirstName))
            return ErrorStrings.General_Error_StringMustNotBeEmpty;

        if (IsStringTooLong(this.FirstName))
            return ErrorStrings.General_Error_StringTooLong50Maximum;

        return null;
    }

    private static bool IsStringMissing(string value)
    {
        return String.IsNullOrEmpty(value) || value.Trim() == String.Empty;
    }

    private static bool IsStringTooLong(string value)
    {
        return value.Length > 50;
    }

    #endregion

【问题讨论】:

    标签: wpf windows validation


    【解决方案1】:

    我认为使用属性更容易。看看我对这个问题的回答:

    How can I define a IDataErrorInfo Error Property for multiple BO properties

    【讨论】:

    • 并且...我还必须为每个属性注释每个 stringempty 和 max50chars 规则...
    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多