我正在尝试使用Detecting WPF Validation Errors中的解决方案在我的WPF应用程序中执行验证。
public static bool IsValid(DependencyObject parent)
{
// Validate all the bindings on the parent
bool valid = true;
LocalValueEnumerator localValues = parent.GetLocalValueEnumerator();
while (localValues.MoveNext())
{
LocalValueEntry entry = localValues.Current;
if (BindingOperations.IsDataBound(parent, entry.Property))
{
Binding binding = BindingOperations.GetBinding(parent, entry.Property);
foreach (ValidationRule rule in binding.ValidationRules)
{
ValidationResult result = rule.Validate(parent.GetValue(entry.Property), null);
if (!result.IsValid)
{
BindingExpression expression = BindingOperations.GetBindingExpression(parent, entry.Property);
System.Windows.Controls.Validation.MarkInvalid(expression, new ValidationError(rule, expression, result.ErrorContent, null));
valid = 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))
{
valid = false;
}
}
return valid;
}
我遇到的问题是当我单步执行TextBox的代码时,我没有得到Text属性。我得到的唯一属性是“PageHeight”,“Instance”和“UndoManagerInstance”。因此,我无法验证TextBox上绑定的规则。
有谁知道我为什么不能获得正确的属性?是否有另一种方法强制WPF中的控件验证?我找不到其他有这个问题的人。
更新 我试图验证的TextBox是在DataTemplate中。我发现,如果我复制其中一个TextBox并将其直接放在Window中,我就能获得数据。使用Woodstock,我看到模板中TextBoxes的数据源是“ParentTemplate”,但它是模板外部TextBox的“Local”。
所以,现在的问题是,如何在 DataTemplate中获取控件的DependencyProperties?
答案 0 :(得分:5)
已经两年多了,但最近我使用相同的方法在同样的问题上苦苦挣扎。
我对该问题的解决方案是使用反射获取对象的所有DependencyProperties,而不是使用不与DataTemplates一起工作的GetLocalValueEnumerator。
代码:
public static bool IsValid(DependencyObject parent)
{
// Validate all the bindings on the parent
bool valid = true;
var infos = parent.GetType().GetFields(
BindingFlags.Public
| BindingFlags.FlattenHierarchy
| BindingFlags.Instance
| BindingFlags.Static).Where(f => f.FieldType == typeof(DependencyProperty));
foreach (FieldInfo field in infos)
{
var dp = (DependencyProperty)field.GetValue(null);
if (BindingOperations.IsDataBound(parent, dp))
{
Binding binding = BindingOperations.GetBinding(parent, dp);
foreach (ValidationRule rule in binding.ValidationRules)
{
ValidationResult result = rule.Validate(parent.GetValue(dp), null);
if (!result.IsValid)
{
BindingExpression expression = BindingOperations.GetBindingExpression(parent, dp);
Validation.MarkInvalid(expression, new ValidationError(rule, expression, result.ErrorContent, null));
valid = 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))
{
valid = false;
}
}
return valid;
}
此代码仅适用于对象拥有的属性,以便为可以使用此代码的附加属性扩展它:
public static List<DependencyProperty> GetAttachedProperties(Object element)
{
List<DependencyProperty> attachedProperties = new List<DependencyProperty>();
System.Windows.Markup.Primitives.MarkupObject markupObject =
System.Windows.Markup.Primitives.MarkupWriter.GetMarkupObjectFor(element);
if (markupObject != null)
{
foreach (System.Windows.Markup.Primitives.MarkupProperty mp in markupObject.Properties)
{
if (mp.IsAttached)
{
attachedProperties.Add(mp.DependencyProperty);
}
}
}
return attachedProperties;
}