Wpf GetLocalValueEnumerator()未返回所有属性

Wpf GetLocalValueEnumerator()未返回所有属性,wpf,validation,data-binding,Wpf,Validation,Data Binding,我正在尝试使用中的解决方案在WPF应用程序中执行验证 我遇到的问题是,当我单步执行文本框的代码时,我没有得到Text属性。我得到的唯一属性是“PageHeight”、“Instance”和“UndoManagerInstance”。因此,我无法验证文本框上绑定的规则 有人知道为什么我得不到正确的属性吗?在WPF中,是否有其他方法强制对控件进行验证?我找不到其他有这个问题的人 更新: 我试图验证的文本框位于DataTemplate中。我发现,如果我复制其中一个文本框并将其直接放在窗口中,我就能够获

我正在尝试使用中的解决方案在WPF应用程序中执行验证

我遇到的问题是,当我单步执行文本框的代码时,我没有得到Text属性。我得到的唯一属性是“PageHeight”、“Instance”和“UndoManagerInstance”。因此,我无法验证文本框上绑定的规则

有人知道为什么我得不到正确的属性吗?在WPF中,是否有其他方法强制对控件进行验证?我找不到其他有这个问题的人

更新: 我试图验证的文本框位于DataTemplate中。我发现,如果我复制其中一个文本框并将其直接放在窗口中,我就能够获得数据。使用Woodstock,我看到模板中文本框的数据源是“ParentTemplate”,但对于模板外的文本框,它是“Local”


所以,现在的问题是,如何在数据模板中获取控件的DependencyProperties?

已经两年多了,但最近我用同样的方法解决了同样的问题

我对该问题的解决方案是使用反射获取对象的所有DependencyProperties,而不是使用GetLocalValueEnumerator,因为GetLocalValueEnumerator不能与DataTemplates协同工作

代码:

此代码仅适用于对象拥有的属性,若要将其扩展为附加属性,可以使用以下代码:

    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;
    }
公共静态列表GetAttachedProperties(对象元素)
{
List attachedProperties=新列表();
System.Windows.Markup.Primitives.MarkupObject MarkupObject=
System.Windows.Markup.Primitives.MarkupWriter.GetMarkupObjectFor(元素);
if(markupObject!=null)
{
foreach(markupObject.property中的System.Windows.Markup.Primitives.MarkupProperty mp)
{
如果(议员已附上)
{
附件属性。添加(mp.DependencyProperty);
}
}
}
归还附件财产;
}

绝妙的解决方案!如何处理带有多个选项卡的
TabControl
?它似乎只更新选定的选项卡。
    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;
    }