Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Standard Attributes

DenisVuyka edited this page Feb 27, 2011 · 1 revision

Standard Attributes

The following standard attributes are supported by WPF PropertyGrid control by default:


BrowsableAttribute attribute specifies whether a property or event should be displayed in the Properties window.

The following class will be taken to demonstrate the attribute usage:

MetadataDemo.cs (based on the sample from Editable Objects)

public class MetadataDemo : NotifiableObject
{
  private string name = "John Doe";
  private string password;      

  public string Name
  {
    get { return name; }
    set
    {
       if (name == null) return;
       name = value;
       OnPropertyChanged("Name");
    }
  }

  public string Password
  {
    get { return password; }
    set
    {
       if (password == value) return;
       password = value;
       OnPropertyChanged("Password");
    }
  }
}

According to the scenario for this sample the business object "MetadataDemo" contains two properties "Name" and "Password" and will be displayed in the Categorized mode. However "Password" property should not be available to users.

The use of [Browsable(false)] attribute should help achieving the required behavior:

MetadataDemo.cs

[Browsable(false)]
public string Password
{
  get { return password; }
  set
  {
    if (password == value) return;
     password = value;
     OnPropertyChanged("Password");
  }
}

As a result the "Password" property will not be available during runtime:


CategoryAttribute attribute specifies the name of the category in which to group the property or event when displayed in a PropertyGrid control set to Categorized mode.

All the properties that do not have this attribute specified are automatically bound to default "Misc" category. CategoryAttribute attribute can be used in order to set the meaningful category the properties should be contained within:

MetadataDemo.cs

[Category("Personal Information")]
public string Name
{
    get { return name; }
    set
    {
       if (name == null) return;
       name = value;
       OnPropertyChanged("Name");
    }
}       

[Category("Personal Information")]
public string Password
{
    get { return password; }
    set
    {
       if (password == value) return;
       password = value;
       OnPropertyChanged("Password");
    }
}


DescriptionAttribute specifies the description for a property or event.

Wpf PropertyGrid control uses descriptions within property tooltips. When no description is provided for the property the "DisplayName" of the property is used. So DescriptionAttribute can be used in order to provide a meaningful tooltips for property items:

MetadataDemo.cs

[Category("Personal Information")]
[Description("Please enter your full name")]
public string Name
{
    get { return name; }
    set
    {
       if (name == null) return;
       name = value;
       OnPropertyChanged("Name");
    }
}

Additionally PropertyGrid control supports the use of DescriptionAttribute for enumerations. It is possible to provide a meaningful and localized values for enumeration class members:

VideoDevices.cs

public enum VideoDevices
{
   UNSPECIFIED,
   [Description("Microsoft VX 6000")]
   MICROSOFT_VX_6000,
   [Description("Logitech Pro 5000")]
   LOGITECH_PRO_5000
};


DisplayNameAttribute attribute specifies the display name for a property, event or public void method which takes no arguments.

PropertyGrid control will detect this attribute and automatically change the "Name" label for the property item.

MetadataDemo.cs

[Category("Personal Information")]
[DisplayName("Login")]
public string Name
{
    get { return name; }
    set
    {
       if (name == null) return;
       name = value;
       OnPropertyChanged("Name");
    }
}


LocalizableAttribute attribute specifies whether a property should be localized.

Note: Property Items carry this attribute however visual support for PropertyGrid will be provided in future versions.


MergablePropertyAttribute attribute specifies that this property can be combined with properties belonging to other objects in a Properties window.

There are many cases when object properties should be defined only individually. For example "Password" property. Such properties can be marked with MergablePropertyAttribute attribute in order to restrict them appear in multiple objects selection scenarios:

MetadataDemo.cs

[Category("Personal Information")]
[MergableProperty(false)]
public string Password
{
    get { return password; }
    set
    {
       if (password == value) return;
       password = value;
       OnPropertyChanged("Password");
    }
}


ParenthesizePropertyNameAttribute indicates whether the name of the associated property is displayed with parentheses in the Properties window.

Use this attribute to add or remove parentheses from property items. Adding parentheses can demonstrated by the following sample:

MetadataDemo.cs

[Category("Personal Information")]
[DisplayName("Login")]
[ParenthesizePropertyName(true)]
public string Name
{
    get { return name; }
    set
    {
       if (name == null) return;
       name = value;
       OnPropertyChanged("Name");
    }
}


ReadOnlyAttribute specifies whether the property this attribute is bound to is read-only or read/write.

Property editors should restrict editing values marked with this attribute. This attribute is valid only for properties (non-Dependency) that have "setters" implemented. Properties that have only "getters" are considered as "read-only" automatically.

Note: custom property editors should also support this attribute.

MetadataDemo.cs

[Category("Personal Information")]
[DisplayName("Login")]
[ReadOnly(true)]
public string Name
{
    get { return name; }
    set
    {
       if (name == null) return;
       name = value;
       OnPropertyChanged("Name");
    }
}

TypeConverterAttribute specifies what type to use as a converter for the object this attribute is bound to.