测试错误和 "other" 值
Test for both errors and "other" values
我目前正在使用 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>
但是,如果输入的值是有效输入但 "out of spec" 或类似值,我还想提供一个视觉指示器(橙色背景?)。对于多个 "error validation" 类型,我有哪些选择?
[使用 .NET Framework 4.5.1]
我最终执行了@Will 的建议。我在每个 IDataErrorInfo
错误字符串的开头插入了一个字符(在这种情况下,我只需要 2 个选项,所以我使用了“0”和“1”)。我创建了 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;
...
}
}
}
一切正常!
我目前正在使用 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>
但是,如果输入的值是有效输入但 "out of spec" 或类似值,我还想提供一个视觉指示器(橙色背景?)。对于多个 "error validation" 类型,我有哪些选择?
[使用 .NET Framework 4.5.1]
我最终执行了@Will 的建议。我在每个 IDataErrorInfo
错误字符串的开头插入了一个字符(在这种情况下,我只需要 2 个选项,所以我使用了“0”和“1”)。我创建了 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;
...
}
}
}
一切正常!