3

If I create a class that extends UserControl and want to set a default value for a DependencyProperty that is declared in UserControl, say FontSize, I can add a static constructor like the following:

static MyUserControl()
{
    UserControl.FontSizeProperty.OverrideMetadata(typeof(MyUserControl), 
new FrameworkPropertyMetadata(28.0));
}

Before I learned about the OverrideMetadata method, I used to override the property and set the DescriptionAttribute in the following way:

public new static readonly DependencyProperty FontSizeProperty = 
DependencyProperty.Register("FontSize", typeof(double), typeof(MyUserControl), 
new PropertyMetadata(28.0));

[Description("My custom description."), Category("Text")]
public new double FontSize
{
    get { return (double)GetValue(FontSizeProperty); }
    set { SetValue(FontSizeProperty, value); }
}

The DescriptionAttribute value is displayed as a popup tooltip in the Properties Window of Visual Studio when a user moves the mouse pointer over the relevant property name. My question is, is it possible to set the DescriptionAttribute value of this DependencyProperty in a similar way to overriding the metadata? Or will I have to keep the CLR getter/setter property and attribute declaration?

Many thanks in advance.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Are you actually asking whether you can change the value of an Attribute, which is in the current assembly, at runtime? Because if so: no, you cannot do that because the attributes are stored with the metadata in the assembly and remain there, statically. – Abel Mar 20 '12 at 23:47
  • @DaveClemmer, please refrain from making pointless edits. Swapping the [tag:dependency-property] tag to [tag:dependency-properties] was a pointless edit and should have been rejected. – Sheridan Jun 13 '14 at 13:52
  • 1
    @Sheridan, first of all this was done quite a long time ago, second this was part of a tag cleanup effort to reduce duplicate tags and improve search, third by changing the tag back to dependency-property, you have created a duplicate tag again (that someone will probably remove again), and finally you have reduced your score in the dependency-properties tag that is actually being used. – Dave Clemmer Jun 15 '14 at 14:36

1 Answers1

3

I found that I could access the DescriptionAttribute value of the inherited type property, but only from an instance constructor and not the static constuctor because I needed a reference to the control object. Also, I could not set it using this method as it was a read only property:

AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["FontSize"].Attributes;
DescriptionAttribute attribute = 
    (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
attribute.Description = "Custom description"; // not possible - read only property

I then discovered that you can't change declared attribute values at run time from these articles:

  1. Can attributes be added dynamically in C#?
  2. Programmatically add an attribute to a method or parameter

I will therefore continue to declare the CLR wrapper properties with new DescriptionAttribute values and override the metadata in the static constructor just to set new default values.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183