GetLocalValueEnumerator () does not return all properties

I am trying to validate in my WPF application using the solution in WPF Validation Error Detection .

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; } 

The problem I am facing is that when I look at the code for the TextBox, I do not get the Text property. The only properties I get are "PageHeight", "Instance" and "UndoManagerInstance". Therefore, I cannot check the rules for binding in a TextBox.

Does anyone have any ideas why I will not get the right properties? Is there any other way to force controls in WPF? I could not find anyone else who had this problem.

Update: The text fields I'm trying to check are within the DataTemplate. I found that if I copy one of the TextBoxes and put it right in the window, I can get the data. Using Woodstock, I saw that the data source for TextBoxes in the template is “ParentTemplate”, but it is “Local” for the TextBox outside the template.

So the question is, how can I get DependencyProperties for controls inside a DataTemplate?

+4
source share
1 answer

It's been over two years, but lately I have been struggling with the same problem using the same method.

My solution to this problem is to get all DependencyProperties of the object using reflection, instead of using GetLocalValueEnumerator, which does not work with DataTemplates.

code:

  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; } 

This code only works with properties belonging to the object, to extend it for nested properties, you can use this code:

  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; } 
+4
source

All Articles