测试错误和"其他"值

时间:2016-08-01 19:29:35

标签: wpf validation mvvm error-handling idataerrorinfo

我目前正在使用MVVM和IDataErrorInfo在一个简单的数据输入应用中验证TextBox es中的输入。如果用户输入非数字,我目前用红色背景标记TextBox

<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Background" Value="Pink"/>
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, 
                    Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

但是,如果输入的值是有效输入但是&#34;超出规范&#34;我还想给出一个视觉指示器(橙色背景?)。或类似的。我可以选择多重&#34;错误验证&#34;类型?

[使用.NET Framework 4.5.1]

1 个答案:

答案 0 :(得分:0)

我最终实施了@Will的建议。我在每个IDataErrorInfo错误字符串的开头插入了一个字符(在这种情况下,我需要的只有2个选项,所以我使用&#34; 0&#34;和&#34; 1&#34;) 。我创建了2 IValueConverter s;每个Background和ToolTip属性都有一个:

public class WarningErrorBkgdConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        char firstChar = ((string)value)[0];
        if (firstChar == '0')
        {
            return Brushes.Pink;  // Error
        }

        Debug.Assert(firstChar == '1', "CANTHAPPEN: Expecting 1st char of string to be 1, was " + firstChar);
        return Brushes.Gold;  // Warning
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}



public class WarningErrorTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // Just ignore the error code
        return ((string)value).Substring(1);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

我修改了原始问题中的TextBox样式以合并这些转换器:

<local:WarningErrorBkgdConverter x:Key="warningErrorBkgdConverter"/>
<local:WarningErrorTextConverter x:Key="warningErrorTextConverter"/>
<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Background" 
                Value="{Binding RelativeSource={RelativeSource Self}, 
                Path=(Validation.Errors)[0].ErrorContent, 
                Converter={StaticResource warningErrorBkgdConverter}}"/>
            <Setter Property="ToolTip" 
                Value="{Binding RelativeSource={RelativeSource Self},
                Path=(Validation.Errors)[0].ErrorContent,
                Converter={StaticResource warningErrorTextConverter}}"/>
        </Trigger>
    </Style.Triggers>
</Style>

然后在我的IDataErrorInfo实现中,示例用法:

public string this[string propertyName]
{
    get
    {
        uint testUint;

        switch (propertyName)
        {
            case "YieldPsi1":
                if (YieldPsi1 == "")
                    return null;

                if (!UInt32.TryParse(YieldPsi1, out testUint))
                    return "0Must be a number";

                if (testUint < 42000)
                    return "1Out of spec";

                return null;
            ...
        }
    }
}

一切顺利!