我正在尝试使用“ 检测WPF验证错误”中的解决方案在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”,“实例”和“ UndoManagerInstance”。因此,我无法验证TextBox上的绑定规则。
有谁知道为什么我无法获得正确的属性?还有另一种方法可以强制对WPF中的控件进行验证吗?我找不到其他遇到此问题的人。
更新: 我要验证的文本框位于DataTemplate中。我发现,如果我复制一个TextBoxes并将其直接放置在Window中,则可以获取数据。使用Woodstock,我看到模板中TextBoxes的数据源是“ ParentTemplate”,但模板之外TextBoxs的数据源是“ Local”。
所以,现在的问题是,我怎么能得到控制DependencyProperties 里面一个DataTemplate?
已经有两年多了,但是最近我正在使用相同的方法来解决相同的问题。
我对该问题的解决方案是使用反射来获取对象的所有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 ListGetAttachedProperties(Object element) { List attachedProperties = new List (); 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; }