C# 获取控件的所有依赖项属性

C# 获取控件的所有依赖项属性,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,我需要确定窗口上的控件是否声明了特定的依赖属性。下面是一个带有DP DemandRole的按钮示例。可以为各种控件类型声明DP,而不仅仅是按钮。我试图枚举窗口上的所有控件,只返回那些声明了DP DemandRole的控件 <Button x:Name="_reset" sec:SecurityAction.DemandRole="Admin,Engineer,SuperUser,Technician,Supervisor" Content="_Reset"

我需要确定窗口上的控件是否声明了特定的依赖属性。下面是一个带有DP DemandRole的按钮示例。可以为各种控件类型声明DP,而不仅仅是按钮。我试图枚举窗口上的所有控件,只返回那些声明了DP DemandRole的控件

<Button x:Name="_reset"
        sec:SecurityAction.DemandRole="Admin,Engineer,SuperUser,Technician,Supervisor" 
        Content="_Reset"
        Visibility="Visible"
        Command="{Binding ResetPasswordCommand}" />

我可以获取特定类型的依赖属性,但这只返回该类型的属性,并且包含我为控件定义的DP

static IEnumerable<FieldInfo> GetDependencyProperties(Type type)
{
        var dependencyProperties = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
                                       .Where(p => p.FieldType.Equals(typeof(DependencyProperty)));
        return dependencyProperties;
    }
静态IEnumerable GetDependencyProperties(类型)
{
var dependencProperties=type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
其中(p=>p.FieldType.Equals(typeof(dependencProperty)));
返回从属属性;
}
知道如何获得控件特定实例上的所有DPs吗

谢谢


兰斯未经测试,但我想你需要旗子

静态IEnumerable GetDependencyProperties(类型)
{
var dependencProperties=type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.flattHierarchy)
其中(p=>p.FieldType.Equals(typeof(dependencProperty)));
返回从属属性;
}

如果不起作用,您需要递归调用
type.BaseType.GetFields
,直到
BaseType
返回
null
并对所有字段进行压缩。

我在Visual Studio论坛的页面上找到了这个代码示例。我不能保证它会起作用,但它“似乎”帮助了最初的问题作者

public static class DependencyObjectHelper
{
    public static List<DependencyProperty> GetDependencyProperties(Object element)
    {
        List<DependencyProperty> properties = new List<DependencyProperty>();
        MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
        if (markupObject != null)
        {
            foreach (MarkupProperty mp in markupObject.Properties)
            {
                if (mp.DependencyProperty != null)
                {
                    properties.Add(mp.DependencyProperty);
                }
            }
        }

        return properties;
    }

    public static List<DependencyProperty> GetAttachedProperties(Object element)
    {
        List<DependencyProperty> attachedProperties = new List<DependencyProperty>();
        MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
        if (markupObject != null)
        {
            foreach (MarkupProperty mp in markupObject.Properties)
            {
                if (mp.IsAttached)
                {
                    attachedProperties.Add(mp.DependencyProperty);
                }
            }
        }

        return attachedProperties;
    }
}
公共静态类DependencyObjectHelper
{
公共静态列表GetDependencyProperties(对象元素)
{
列表属性=新列表();
MarkupObject MarkupObject=MarkupWriter.GetMarkupObjectFor(元素);
if(markupObject!=null)
{
foreach(markupObject.property中的MarkupProperty mp)
{
if(mp.dependencProperty!=null)
{
添加(mp.DependencyProperty);
}
}
}
归还财产;
}
公共静态列表GetAttachedProperties(对象元素)
{
List attachedProperties=新列表();
MarkupObject MarkupObject=MarkupWriter.GetMarkupObjectFor(元素);
if(markupObject!=null)
{
foreach(markupObject.property中的MarkupProperty mp)
{
如果(议员已附上)
{
附件属性。添加(mp.DependencyProperty);
}
}
}
归还附件财产;
}
}
如果这些扩展方法没有帮助,链接页面上还有其他示例


更新>>>

我刚刚在堆栈溢出上发现了这个问题,这可能也有帮助:


那么简而言之,您需要类型及其基类型的所有依赖属性吗?是吗?谢谢谢里登,你添加的更新链接效果很好!!很乐意帮忙。在这种情况下,我将关闭这个问题,作为堆栈溢出上首选的链接问题的副本。FlatterHierachy没有返回DP,递归地遍历基类型也没有返回DP。
public static class DependencyObjectHelper
{
    public static List<DependencyProperty> GetDependencyProperties(Object element)
    {
        List<DependencyProperty> properties = new List<DependencyProperty>();
        MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
        if (markupObject != null)
        {
            foreach (MarkupProperty mp in markupObject.Properties)
            {
                if (mp.DependencyProperty != null)
                {
                    properties.Add(mp.DependencyProperty);
                }
            }
        }

        return properties;
    }

    public static List<DependencyProperty> GetAttachedProperties(Object element)
    {
        List<DependencyProperty> attachedProperties = new List<DependencyProperty>();
        MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
        if (markupObject != null)
        {
            foreach (MarkupProperty mp in markupObject.Properties)
            {
                if (mp.IsAttached)
                {
                    attachedProperties.Add(mp.DependencyProperty);
                }
            }
        }

        return attachedProperties;
    }
}