使用 IValueConverter 验证可为 null int
我正在尝试执行验证属性。我们有一个可以为 null 的属性,名为:
public int? Number
{
get { return _number; }
set
{
if (_number != value)
{
_number = value;
RaisePropertyChanged("Number");
}
}
}
并且该属性绑定到文本框。我只想验证这两个场景:
- 假设用户将文本框留空(textbox.text=""),因此 Number 属性必须接收 null 值(而不是 "" )。
- 如果用户输入“2b”,Number 属性必须具有空值(因为这是一个错误),但文本框仍必须显示“2b”。
所以这个的实现是:
public class IntConverter : IValueConverter
{
private static readonly IntConverter defaultInstance = new IntConverter();
public static IntConverter Default { get { return defaultInstance; } }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is int?)
{
int? intValue = (int?)value;
if (intValue.HasValue)
{
return intValue.Value.ToString();
}
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is string)
{
int number;
if (Int32.TryParse((string)value, out number))
{
return number;
}
}
return null;
}
}
上面的代码确实有效,但只有一件事做得不好。当用户输入“2b”时,此时应该显示错误(红色边框)。我该如何修复它?
注意:验证属性为 true。
<TextBox Text="{Binding Number, UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True, TargetNullValue={x:Static sys:String.Empty},
Converter={x:Static c:IntConverter.Default}}" />
I'm trying to perform a validation property. We have a nullable property called:
public int? Number
{
get { return _number; }
set
{
if (_number != value)
{
_number = value;
RaisePropertyChanged("Number");
}
}
}
And this property is bound to a textbox. I only want to validate this two escenarios:
- Imagine that the user left the textbox empty (textbox.text=""), so Number property must receive null value (instead "" ).
- And if the user inputs "2b", Number property must have a null value (because is an error), but the textbox must still say "2b".
So the implementation for this is:
public class IntConverter : IValueConverter
{
private static readonly IntConverter defaultInstance = new IntConverter();
public static IntConverter Default { get { return defaultInstance; } }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is int?)
{
int? intValue = (int?)value;
if (intValue.HasValue)
{
return intValue.Value.ToString();
}
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is string)
{
int number;
if (Int32.TryParse((string)value, out number))
{
return number;
}
}
return null;
}
}
The code above is really working, but just one thing is not doing good. When the user inputs "2b", at this moment, should show the error (red border). How can I fix it?
NOTE: Validations properties are in true.
<TextBox Text="{Binding Number, UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True, TargetNullValue={x:Static sys:String.Empty},
Converter={x:Static c:IntConverter.Default}}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在视图模型类中实现 IDataErrorInfo 接口,而不是使用 NullableIntValidation 类。
有一个很好的例子这里。
Implement the IDataErrorInfo interface in your view model class instead of using the NullableIntValidation class.
There is a nice example here.
为什么你需要一个转换器呢,WPF 绑定足够强大来处理这种情况。
首先如果你用int绑定textBox? value 并且您尝试将字符串设置为它(2b),它会自动在其周围显示红色验证错误边框。
其次,如果您想在空字符串的情况下设置 null 值,则只需为绑定设置
TargetNullValue
即可。此代码示例将适合您 -
确保将命名空间系统命名空间添加到您的 xaml -
Why do you need a converter for this, WPF binding is powerful enough to handle this case.
First of all if you bind the textBox with int? value and you try to set the string to it (2b), it will automatically show red validation error border around it.
Second if you want to set the null value in case of Empty string, all you need to set the
TargetNullValue
for your binding.This code sample will work for you -
Make sure you add the namespace system namespace to your xaml -