开发者

Is there a list of Attribute classes that can be applied to a DependencyProperty?

开发者 https://www.devze.com 2022-12-17 12:16 出处:网络
I\'m working on a problem that seems like it might be solved by applying an Attribute to the DependencyProperty.I got curious and I can\'t find documentation that lists all the XXXAttribute classes th

I'm working on a problem that seems like it might be solved by applying an Attribute to the DependencyProperty. I got curious and I can't find documentation that lists all the XXXAttribute classes that apply to DependencyProperties and what the attributes are used for. Does anyone know of anything like that? If not, maybe post some attributes that you have used and what you've used them for in the past? Maybe there a开发者_如何学编程ren't any?


This are no Attributes that I know of which are designed to adorn a Dependency property.

Of course you can adorn the class Property that is using the Dependency property or the even the static field where the reference to the DependencyProperty is held:-

public class MyClass : DependencyObject
{
    [PossibleLocationForPropertyAttribute()]
    public string Description
    {
        get { return (string)GetValue(DescriptionProperty); }
        set { SetValue(DescriptionProperty, value); }
    }

    [PossibleLocationForFieldAttribute()]
    public static readonly DependencyProperty DescriptionProperty =
        DependencyProperty.Register("Description", typeof(string), typeof(MyClass), null);
}

However neither of these help you when all you have is a reference to a dependency property. Unfortunately there is no way that you can attach additional data to a dependency property that is retrievable without knowledge to the type against which it is registered.

I've never actually tried this but...

public MyExtendedPropertyMetaData : PropertyMetaData
{
    public object Token {get; private set;}

    public MyExtendedPropertyMetaData(object token) : PropertyMetaData(null)
    {
        Token = token;
    }

}

public static readonly DependencyProperty DescriptionProperty =
        DependencyProperty.Register("Description", typeof(string), typeof(MyClass),
          new MyExtendedMetaData("Some token could be anything"));

Now given simply a DP and that you know its registered to MyClass then:-

var meta = dp.GetMetaData(typeof(MyClass)) as MyExtendedPropertyMetadata;
string tokenData = (string)meta.Token;

If do know the type against which the DP is registered then the above is actually quite tidy compared to Attribute usage. If you don't then nothing will help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消