【问题标题】:Get all Dependency Properties for a control [duplicate]获取控件的所有依赖属性[重复]
【发布时间】:2014-12-22 13:45:27
【问题描述】:

我需要确定我的窗口上的控件是否声明了特定的依赖属性。下面是一个带有 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;
    }

知道如何在控件的特定实例上获取所有 DP 吗?

谢谢,

兰斯

【问题讨论】:

  • 那么简而言之,您需要类型及其基类型的所有依赖属性吗?是吗?

标签: c# wpf dependency-properties


【解决方案1】:

我从 Visual Studio 论坛的 Getting list of all dependency/attached properties of an Object 页面找到了这个代码示例。我不能保证它会起作用,但它“似乎”帮助了原始问题作者。

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

如果这些扩展方法没有帮助,链接页面上还有其他示例。


更新>>>

我刚刚在 Stack Overflow 上发现了这个问题,可能也有帮助:

How to enumerate all dependency properties of control?

【讨论】:

  • 感谢 Sheridan,您添加的更新链接效果很好!
  • 很高兴为您提供帮助。在这种情况下,我会将这个问题作为 Stack Overflow 上首选的链接问题的副本关闭。
【解决方案2】:

未测试,但我猜你需要BindingFlags.FlattenHierarchy 标志。

static IEnumerable<FieldInfo> GetDependencyProperties(Type type)
{
     var dependencyProperties = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy)
                                       .Where(p => p.FieldType.Equals(typeof(DependencyProperty)));
     return dependencyProperties;
}

如果这不起作用,您需要递归调用type.BaseType.GetFields 直到BaseType 返回null 并连接所有字段。

【讨论】:

  • FlattenHierachy 没有返回 DP,递归遍历基本类型也没有返回 DP。
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 2013-04-05
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多