如何枚举控件的所有依赖属性?

时间:2022-10-30 10:37:17

I have some WPF control. For example, TextBox. How to enumerate all dependency properties of that control (like XAML editor does)?

我有一些WPF控件。例如,TextBox。如何枚举该控件的所有依赖属性(如XAML编辑器那样)?

5 个解决方案

#1


8  

public IList<DependencyProperty> GetAttachedProperties(DependencyObject obj)
{
    List<DependencyProperty> result = new List<DependencyProperty>();

    foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(obj,
        new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }))
    {
        DependencyPropertyDescriptor dpd =
            DependencyPropertyDescriptor.FromProperty(pd);

        if (dpd != null)
        {
            result.Add(dpd.DependencyProperty);
        }
    }

    return result;
}

Found here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/580234cb-e870-4af1-9a91-3e3ba118c89c

在此处找到:http://social.msdn.microsoft.com/Forums/en/wpf/thread/580234cb-e870-4af1-9a91-3e3ba118c89c

#2


7  

Using reflection is not needed (and a bad idead IMHO) because the framework has already utility classes for this (but they're not obvious to find :-).

不需要使用反射(以及错误的idead恕我直言),因为框架已经有了实用程序类(但它们并不明显可以找到:-)。

Here is an answer based on this article: Enumerate Bindings and the LocalValueEnumerator Structure

以下是基于本文的答案:枚举绑定和LocalValueEnumerator结构

    public static IEnumerable<DependencyProperty> EnumerateDependencyProperties(this DependencyObject obj)
    {
        if (obj != null)
        {
            LocalValueEnumerator lve = obj.GetLocalValueEnumerator();
            while (lve.MoveNext())
            {
                yield return lve.Current.Property;
            }
        }
    }

Here is another answer based on this other article: Getting list of all dependency/attached properties of an Object that uses MarkupWriter.GetMarkupObjectFor Method.

这是基于另一篇文章的另一个答案:获取使用MarkupWriter.GetMarkupObjectFor方法的Object的所有依赖项/附加属性的列表。

    public static IEnumerable<DependencyProperty> EnumerateDependencyProperties(object element)
    {
        if (element != null)
        {
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.DependencyProperty != null)
                        yield return mp.DependencyProperty;
                }
            }
        }
    }

    public static IEnumerable<DependencyProperty> EnumerateAttachedProperties(object element)
    {
        if (element != null)
        {
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.IsAttached)
                        yield return mp.DependencyProperty;
                }
            }
        }
    }

#3


5  

You can use reflection, via the GetFields, method to find all the public static properties on TextBox. You can then use a Linq Where clause to filter these to any of type DependencyProperty:

您可以通过GetFields方法使用反射来查找TextBox上的所有公共静态属性。然后,您可以使用Linq Where子句将这些子句过滤为任何DependencyProperty类型:

  var flags = BindingFlags.Static |
              BindingFlags.FlattenHierarchy |
              BindingFlags.Public;
  var dependencyProperties = typeof(TextBox).GetFields(flags)
                     .Where(f => f.FieldType == typeof(DependencyProperty));

You can then transform this to a list of names via a Select:

然后,您可以通过选择将其转换为名称列表:

  var dependencyProperties = typeof(TextBox).GetFields(flags)
                     .Where(f => f.FieldType == typeof(DependencyProperty))
                     .Select(dp => dp.Name);

Note: each name has a 'Property' suffix, you can of course remove this in the above Select clause if you like.

注意:每个名称都有一个'Property'后缀,如果您愿意,您当然可以在上面的Select子句中删除它。

#4


1  

If you want name of the DependencyProperties of an element then you can do this:

如果您想要元素的DependencyProperties的名称,那么您可以这样做:

    var results = from prop in typeof(element).GetFields() 
                  where prop.FieldType == typeof(DependencyProperty)
                  select prop.Name.Substring(0, prop.Name.Length - 8);

where 8 is length of the string "Property" which appears at the end of a dependency property!

其中8是字符串“Property”的长度,它出现在依赖属性的末尾!

#5


0  

Try

尝试

        var fieldInfos = typeof(TextBox).GetFields( BindingFlags.Public | BindingFlags.Static).Where(x=>x.FieldType == typeof(DependencyProperty));
        foreach (var fieldInfo in fieldInfos)
        {
            Console.WriteLine(fieldInfo.Name);
        }

#1


8  

public IList<DependencyProperty> GetAttachedProperties(DependencyObject obj)
{
    List<DependencyProperty> result = new List<DependencyProperty>();

    foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(obj,
        new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) }))
    {
        DependencyPropertyDescriptor dpd =
            DependencyPropertyDescriptor.FromProperty(pd);

        if (dpd != null)
        {
            result.Add(dpd.DependencyProperty);
        }
    }

    return result;
}

Found here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/580234cb-e870-4af1-9a91-3e3ba118c89c

在此处找到:http://social.msdn.microsoft.com/Forums/en/wpf/thread/580234cb-e870-4af1-9a91-3e3ba118c89c

#2


7  

Using reflection is not needed (and a bad idead IMHO) because the framework has already utility classes for this (but they're not obvious to find :-).

不需要使用反射(以及错误的idead恕我直言),因为框架已经有了实用程序类(但它们并不明显可以找到:-)。

Here is an answer based on this article: Enumerate Bindings and the LocalValueEnumerator Structure

以下是基于本文的答案:枚举绑定和LocalValueEnumerator结构

    public static IEnumerable<DependencyProperty> EnumerateDependencyProperties(this DependencyObject obj)
    {
        if (obj != null)
        {
            LocalValueEnumerator lve = obj.GetLocalValueEnumerator();
            while (lve.MoveNext())
            {
                yield return lve.Current.Property;
            }
        }
    }

Here is another answer based on this other article: Getting list of all dependency/attached properties of an Object that uses MarkupWriter.GetMarkupObjectFor Method.

这是基于另一篇文章的另一个答案:获取使用MarkupWriter.GetMarkupObjectFor方法的Object的所有依赖项/附加属性的列表。

    public static IEnumerable<DependencyProperty> EnumerateDependencyProperties(object element)
    {
        if (element != null)
        {
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.DependencyProperty != null)
                        yield return mp.DependencyProperty;
                }
            }
        }
    }

    public static IEnumerable<DependencyProperty> EnumerateAttachedProperties(object element)
    {
        if (element != null)
        {
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.IsAttached)
                        yield return mp.DependencyProperty;
                }
            }
        }
    }

#3


5  

You can use reflection, via the GetFields, method to find all the public static properties on TextBox. You can then use a Linq Where clause to filter these to any of type DependencyProperty:

您可以通过GetFields方法使用反射来查找TextBox上的所有公共静态属性。然后,您可以使用Linq Where子句将这些子句过滤为任何DependencyProperty类型:

  var flags = BindingFlags.Static |
              BindingFlags.FlattenHierarchy |
              BindingFlags.Public;
  var dependencyProperties = typeof(TextBox).GetFields(flags)
                     .Where(f => f.FieldType == typeof(DependencyProperty));

You can then transform this to a list of names via a Select:

然后,您可以通过选择将其转换为名称列表:

  var dependencyProperties = typeof(TextBox).GetFields(flags)
                     .Where(f => f.FieldType == typeof(DependencyProperty))
                     .Select(dp => dp.Name);

Note: each name has a 'Property' suffix, you can of course remove this in the above Select clause if you like.

注意:每个名称都有一个'Property'后缀,如果您愿意,您当然可以在上面的Select子句中删除它。

#4


1  

If you want name of the DependencyProperties of an element then you can do this:

如果您想要元素的DependencyProperties的名称,那么您可以这样做:

    var results = from prop in typeof(element).GetFields() 
                  where prop.FieldType == typeof(DependencyProperty)
                  select prop.Name.Substring(0, prop.Name.Length - 8);

where 8 is length of the string "Property" which appears at the end of a dependency property!

其中8是字符串“Property”的长度,它出现在依赖属性的末尾!

#5


0  

Try

尝试

        var fieldInfos = typeof(TextBox).GetFields( BindingFlags.Public | BindingFlags.Static).Where(x=>x.FieldType == typeof(DependencyProperty));
        foreach (var fieldInfo in fieldInfos)
        {
            Console.WriteLine(fieldInfo.Name);
        }