GetLocalValueEnumerator() Not Returning All Properties

Posted by a_hardin on Stack Overflow See other posts from Stack Overflow or by a_hardin
Published on 2008-12-03T20:01:38Z Indexed on 2011/01/10 10:53 UTC
Read the original article Hit count: 137

Filed under:
|
|

I am trying to perform validation in my WPF application using the solution in Detecting WPF Validation Errors.

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 running into is that when I step through the code for a TextBox, I'm not getting the Text property. The only properties I get are "PageHeight", "Instance", and "UndoManagerInstance". Therefore, I can not Validate the rules for the binding on the TextBox.

Does anyone have any idea why I wouldn't be getting the correct properties? Is there another way to force validaton on controls in WPF? I haven't been able to find anyone else who has had this problem.

Update: The TextBoxes I am trying to validate are within a DataTemplate. I found that if I copy one of the TextBoxes and place it directly in the Window, I am able to get the data. Using Woodstock, I saw that the data source for the TextBoxes in the template is "ParentTemplate", but it's "Local" for the TextBox outside of the template.

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

© Stack Overflow or respective owner

Related posts about wpf

Related posts about validation