Click here to Skip to main content
15,886,077 members
Articles / Programming Languages / C#
Article

PropertyGridEx

Rate me:
Please Sign up or sign in to vote.
2.20/5 (6 votes)
16 Nov 20064 min read 81K   1.4K   39   13
A control which extends the functionality of the PropertyGrid.

Introduction

In my last article, I described a control that extends the functionality of the System.Windows.Forms.PropertyGrid. Now, let’s demonstrate the control. This diagram illustrates how the system property grid displays items:

Image 1

Activity Diagram 1

Activity Diagram 1

  1. PropertGrid selects an object derived from the System.ComponentModel.ICustomTypeDescriptor interface used by the SelectedObject property.
  2. PropertGrid invokes the ICustomTypeDescriptor::GetProperties() method and gets the System.ComponentModel.PropertyDescriptorCollection object that contains the collection of System.ComponentModel.PropertyDescriptor objects.
  3. Using the methods and properties of the System.ComponentModel.PropertyDescriptor object, PropertGrid gets the description of each item (for example: display name, value, and others).
  4. If an item contains sub-items, then the PropertyDescriptor object should return an appropriate object derived from the System.ComponentModel.ICustomTypeDescriptor interface that describes a child item, through the GetValue() method. If an item has a simple system type, then this method returns just one.

Examine how the Puma.Controls.PropertyGrid provides the necessary data for the system property grid:

Image 3

Here is a reference of the extended interfaces:

IPropertyGridItemEx is derived from IPropertyGridItem

Provides auxiliary information about the item, most essential being the appropriate PropertyDescriptor object that is used by the property grid for displaying the item (see how the system property grid displays an item).

Has the following properties and methods:

Properties:

  • System.ComponentModel.PropertyDescriptor PropertyDescriptor {get;}

    Returns the PropertyDescriptor object that describes the item.

  • IPropertyGridItemsEx ChildsEx {get;}

    Returns the IPropertyGridItemsEx interface for the item. If the interface has not yet been created, return null.

  • IPropertyGridItemRootEx RootItemEx {get;}

    Returns the root item of the items tree.

  • IPropertyGridItem BaseItem {get;}

    Returns the base IPropertyGridItem interface for this item.

  • int Index {get;}

    Returns the index of the element in the parent’s child fragment collection.

    Remark:

    This is the index relative to the first item in the fragment (see the IPropertyGridChildItemsFragment description), not to the child container (see the IPropertyGridChildItems description).

  • bool IsCategory {get;}

    Returns a flag indicating whether the item represents a category item.

    Remark:

    IsCategory returns true if this item is a sub-root item. In this case, when the property grid is displayed in Category mode and PropertyGrid.FirstItemsAsCategories = true, then the item is displayed as the category for all of its child items.

Methods:

  • IPropertyGridItemsEx CreateChildsEx()

    Creates and returns the IPropertyGridItemsEx interface for the item.

  • string OnValueValidate(string Value);

    Validates the value changed by the user before it is recorded by the property grid. Accepts a new value from the user, validates it, reformats it if necessary, and returns.

IPropertyGridChildItemsEx derived from IPropertyGridChildItems

Properties:

  • System.ComponentModel.PropertyDesriptorCollection PropertyDesriptorCollection {get;}

    Provides appropriately formatted information for the property grid about the child of the item.

Puma.Controls.PropertyGridEx activity diagram:

Activity Diagram 2

Activity Diagram 2

Notice:

  1. When the diagram indicates what should be created for an interface, this means that a class is created that implements this interface (in the Puma.Control.PropertyGridEx project, these classes are PropertyGridChildItemsExImpl and PropertyGridItemExImpl for IPropertyGridChildItemsEx and IPropertyGridItemEx, respectively).

    It is clear that not all sequenced activities that accomplish these interfaces are strictly defined. What is important for the robust functionality of this system is that all members of the base interface are implemented, in what manner does not matter.

  2. PropertyGrid.GetProperties returns a collection which contains a PropertyDescriptor object whose GetValue() method returns a PropertyGridItem object or simply a string value. If it returns a PropertyGridItem object, execute all sequence of actions in the same order, beginning from the PropertyGrid.GetProperties operation.

    When an item displayed by the property grid invokes its methods and properties of the appropriate PropertyDescriptor object (see Activity diagram 1), for the purpose of this project, all these invocations are delegated to the PropertyGridItemEx object (see Activity diagram 2), since we want to change the default representation of an item in the property grid with the appropriate overridden methods and properties of the PropertyGridItemEx object. This is why we need to derive a class from the PropertyGridItemEx class and override the necessary properties.

Example:

C#
public class NewPropertyGridItemExImpl : 
       Puma.Controls.PropertyGrid.PropertyGridItemExImpl
{
    ...
    public override string Value
    {
        get
        {
            //All values in property grid are displayed with 
            //prefix [Changed value representation]
            return "[Changed value representation]" + base.Value;
        }
        set
        {
            base.Value = value;
        }
    }
    ... 

Now implement this class used by the property grid in the place of the base class. This should override the PropertyGridItemRootExImpl, PropertyGridItemExImpl, and PropertyGridChildItemsExImpl classes.

Schematically:

Image 5

To see how this class is implemented in the current project, see the PropertyGridItemExImpl.cs, PropertyGridItemRootExImpl.cs, and the PropertyGridChildItemsExImpl.cs files.

Thanks to Arnie Berg for his contribution in this article writing.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicensing inquiry Pin
2twotango10-Feb-09 3:11
2twotango10-Feb-09 3:11 
QuestionGetting Invalid data errors - how do I catch them? Pin
BatVink6-Mar-08 3:23
professionalBatVink6-Mar-08 3:23 
QuestionHow clearing any item of the propertyGrid Pin
Poletto6-Sep-07 4:41
Poletto6-Sep-07 4:41 
I want purely remove any item once the propertygrid is loaded with a xml file.
Using your dialog, try to load a xml file and, later, to reload an other or the same xml file.
It will not load beucase an exception occurs.
Can you suggest where is the problem ?
GeneralSetting ReadOnly property Pin
Poletto29-Aug-07 3:42
Poletto29-Aug-07 3:42 
QuestionPropertyGridItem doesn't work correctly [modified] Pin
nomats23-Aug-07 2:29
nomats23-Aug-07 2:29 
AnswerRe: PropertyGridItem doesn't work correctly Pin
nomats23-Aug-07 21:01
nomats23-Aug-07 21:01 
GeneralPropertygridex and IsResdOnly property Pin
fvergara9-Jul-07 20:40
fvergara9-Jul-07 20:40 
Generalpropertygridex and isreadonly property Pin
fvergara8-Jul-07 23:06
fvergara8-Jul-07 23:06 
GeneralRe: propertygridex and isreadonly property Pin
El'Cachubrey9-Jul-07 4:26
El'Cachubrey9-Jul-07 4:26 
GeneralMany used methods and Porperty are deprecated with .NET 2.0 Pin
VirginF12-Dec-06 3:24
VirginF12-Dec-06 3:24 
GeneralRe: Many used methods and Porperty are deprecated with .NET 2.0 Pin
El'Cachubrey12-Dec-06 22:27
El'Cachubrey12-Dec-06 22:27 
GeneralAggregation Vs Composite Pin
Nirosh30-Nov-06 18:12
professionalNirosh30-Nov-06 18:12 
GeneralRe: Aggregation Vs Composite Pin
El'Cachubrey12-Dec-06 22:34
El'Cachubrey12-Dec-06 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.