public class CountValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
double d = 0.0;
if (double.TryParse((string)value, out d) )
{
return new ValidationResult(true, "OK");
}
else
{
return new ValidationResult(false, "无法解析输入");
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
引入规则
使用规则
如果需要校验界面是否包含未解决的报错可以获取页面所有的报错信息
public bool AreAllValidationsSucceeded(DependencyObject dependencyObject)
{
bool allValidationsSucceeded = true;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
if (child is FrameworkElement)
{
FrameworkElement frameworkElement = child as FrameworkElement;
// 检查绑定错误
if (Validation.GetHasError(frameworkElement))
{
allValidationsSucceeded = false;
break; // 一旦发现有错误,就不再遍历
}
}
// 递归遍历子元素
if (!AreAllValidationsSucceeded(child))
{
allValidationsSucceeded = false;
break;
}
}
return allValidationsSucceeded;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
调用
留待后查,同时方便他人