using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace erpcli.Lib
{
static class Validator
{
// LINQ version
public static bool IsValid(DependencyObject obj)
{
// The dependency object is valid if it has no errors and all
// of its children (that are dependency objects) are error-free.
// Reference: http://stackoverflow.com/questions/127477/detecting-wpf-validation-errors
return !Validation.GetHasError(obj) &&
LogicalTreeHelper.GetChildren(obj)
.OfType<DependencyObject>()
.All(IsValid);
}
/*
public static bool IsValid(DependencyObject parent)
{
if (Validation.GetHasError(parent))
return false;
// Validate all the bindings on the children
for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (!IsValid(child)) { return false; }
}
return true;
}
*/
}
}
Usage:
private void saveButton_Click(object sender, RoutedEventArgs e)
{
if (Validator.IsValid(this.MyStackPanel)) // is valid
{
....
}
}
Reference:
http://stackoverflow.com/questions/127477/detecting-wpf-validation-errors
No comments:
Post a Comment