float.NaN 的 DataAnnotations 属性
DataAnnotations attribute for float.NaN
我正在使用 DataAnnotations
来验证我的 ViewModel
(WPF)
这样我就可以控制某些按钮的启用状态,如果用户输入的不是浮动的东西并且如果值超出范围。
[Range(0, float.MaxValue, ErrorMessage = "StepSize must be more than 0")]
public float StepSize
{
get { return model.StepSize; }
set
{
model.StepSize = value;
RaisePropertyChangedAndRevalidate(nameof(StepSize));
}
}
private void RaisePropertyChangedAndRevalidate(string propertyName)
{
RaisePropertyChanged(propertyName);
if (_fieldBindingErrors == 0)
{
RaisePropertyChanged(nameof(IsValid));
}
}
private int _fieldBindingErrors = 0;
public ICommand RegisterFieldBindingErrors
{
get
{
return new RelayCommand<ValidationErrorEventArgs>(e =>
{
if (e.Action == ValidationErrorEventAction.Added)
{
_fieldBindingErrors++;
}
else
{
_fieldBindingErrors--;
}
RaisePropertyChanged(nameof(IsValid));
});
}
}
public bool IsValid
{
get
{
bool valid = Validator.TryValidateObject(this,
new ValidationContext(this, null, null),
new List<System.ComponentModel.DataAnnotations.ValidationResult>())
&& _fieldBindingErrors == 0;
return valid;
}
}
问题是,当我将 textbox.Text 字符串转换为浮点数时,我必须将值设置为无效的值(在未使用 Range 属性时也适用于一般情况)。所以我将值设置为float.NaN。
问题是 NaN 似乎是一个有效的浮点数,它甚至通过了上面的 Range 验证。
有没有我可以用来验证该值不是 NaN 的属性?
找到解决方案,我改为创建自定义验证规则并将其应用于 XAML。
class StepSizeValidationRule : ValidationRule
{
private int _min;
private int _max;
public StepSizeValidationRule()
{
}
public int Min
{
get { return _min; }
set { _min = value; }
}
public int Max
{
get { return _max; }
set { _max = value; }
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
float stepSize = 0;
try
{
if (!float.TryParse(value.ToString(), out stepSize))
{
return new ValidationResult(false, "Not a float");
}
}
catch (Exception e)
{
return new ValidationResult(false, "Not a float " + e.Message);
}
if (float.IsNaN(stepSize))
{
return new ValidationResult(false, "Not a float ");
}
if ((stepSize < Min) || (stepSize > Max))
{
return new ValidationResult(false,
"Please enter a stepSize in the range: " + Min + " - " + Max + ".");
}
else
{
return new ValidationResult(true, null);
}
}
}
我正在使用 DataAnnotations
来验证我的 ViewModel
(WPF)
这样我就可以控制某些按钮的启用状态,如果用户输入的不是浮动的东西并且如果值超出范围。
[Range(0, float.MaxValue, ErrorMessage = "StepSize must be more than 0")]
public float StepSize
{
get { return model.StepSize; }
set
{
model.StepSize = value;
RaisePropertyChangedAndRevalidate(nameof(StepSize));
}
}
private void RaisePropertyChangedAndRevalidate(string propertyName)
{
RaisePropertyChanged(propertyName);
if (_fieldBindingErrors == 0)
{
RaisePropertyChanged(nameof(IsValid));
}
}
private int _fieldBindingErrors = 0;
public ICommand RegisterFieldBindingErrors
{
get
{
return new RelayCommand<ValidationErrorEventArgs>(e =>
{
if (e.Action == ValidationErrorEventAction.Added)
{
_fieldBindingErrors++;
}
else
{
_fieldBindingErrors--;
}
RaisePropertyChanged(nameof(IsValid));
});
}
}
public bool IsValid
{
get
{
bool valid = Validator.TryValidateObject(this,
new ValidationContext(this, null, null),
new List<System.ComponentModel.DataAnnotations.ValidationResult>())
&& _fieldBindingErrors == 0;
return valid;
}
}
问题是,当我将 textbox.Text 字符串转换为浮点数时,我必须将值设置为无效的值(在未使用 Range 属性时也适用于一般情况)。所以我将值设置为float.NaN。 问题是 NaN 似乎是一个有效的浮点数,它甚至通过了上面的 Range 验证。
有没有我可以用来验证该值不是 NaN 的属性?
找到解决方案,我改为创建自定义验证规则并将其应用于 XAML。
class StepSizeValidationRule : ValidationRule { private int _min; private int _max;
public StepSizeValidationRule() { } public int Min { get { return _min; } set { _min = value; } } public int Max { get { return _max; } set { _max = value; } } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { float stepSize = 0; try { if (!float.TryParse(value.ToString(), out stepSize)) { return new ValidationResult(false, "Not a float"); } } catch (Exception e) { return new ValidationResult(false, "Not a float " + e.Message); } if (float.IsNaN(stepSize)) { return new ValidationResult(false, "Not a float "); } if ((stepSize < Min) || (stepSize > Max)) { return new ValidationResult(false, "Please enter a stepSize in the range: " + Min + " - " + Max + "."); } else { return new ValidationResult(true, null); } } }