Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's more than two years but recently I was struggling with the same problem using the same method. </p> <p>My solution to that problem is to get all DependencyProperties of object using reflection instead of using GetLocalValueEnumerator which is not working corectlly with DataTemplates.</p> <p>Code:</p> <pre><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 =&gt; 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; } </code></pre> <p>This code works only with Properties owned by the object to extend it for attached properties you can use this code:</p> <pre><code> public static List&lt;DependencyProperty&gt; GetAttachedProperties(Object element) { List&lt;DependencyProperty&gt; attachedProperties = new List&lt;DependencyProperty&gt;(); 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload