The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  75 Posts :: 0 Stories :: 49 Comments :: 2 Trackbacks

Ads

Archives

Post Categories

Open Source Projects

Other Blogs

The Microsoft Property Grid control is great for displaying properties of your types, but using it for displaying your own custom property lists is slightly more tricky. Included is an example and source code for implementing your own PropertyDescriptor class for use with the control.

Download

PropertyGridDemo.zip

The Code

There are 3 classes that you'll ned to implement to make use of the functionality provided by the PropertyGrid control:

  1. A CustomProperty class, which in this case holds a category, name and value (and some other attributes that are useful)
  2. A CustomPropertyDescriptor class, which inherits System.Component.PropertyDescriptor. This class 'extracts' the necessary information from your custom property class for the grid to display.
  3. A CustomPropertyList, which inherits from List<CustomProperty> as well as inherits ICustomTypeDescriptor. This is the class that you will use as the datasource for the property grid.  

Here is a snippet of the CustomProperty class; I've added an IsVisible and an IsReadOnly property that we can use in the grid.

public class CustomProperty
{
    public CustomProperty(string category, string name, string value, bool isVisible, bool isReadOnly)
    {
        Category = category;
        Name = name;
        Value = value;
        IsVisible = isVisible;
        IsReadOnly = isReadOnly;
    }
 
    public string Category { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public bool IsVisible { get; set; }
    public bool IsReadOnly { get; set; }
}

The other two classes included in the demo project are pretty straight forward, so download it and have a look. The end result is something like this:

The property grid control with your own properties

The "Details, Stuff and Things" sections come from the Category property in your custom class, and the name and values from their respective properties.

posted on Friday, February 22, 2008 4:55 AM

Feedback

# re: The Microsoft Property Grid control - a custom use 2/28/2008 12:36 PM Fry
An interesting control to use that could open up many more UI perspectives than the ones programmers currently associate it with.

Think of it: fewer lable, textbox combination...

Hey, that could also be quite nice to use in a Code generator! Listing properties of the different objects that will be generated!

Comments have been closed on this topic.