In This Article

Collections

The property grid supports inline collection display and even editing in some cases.

Collection Property Display Mode

By default, collections are simply displayed using their associated type converter. Out of the box, only the type converter for arrays (ArrayConverter) allows expansion. The default type converter used for other collection types, such as lists and dictionaries, does not allow expansion. The PropertyGrid.CollectionPropertyDisplayMode property can be used to override this behavior.

Screenshot

A property grid showing several collections using Default

If PropertyGrid.CollectionPropertyDisplayMode is set to Expandable, the type converter for supported collection types will be overridden to allow expansion. In addition, the item count of the collection is used when converting a collection to a string (as seen in the following image).

Screenshot

A property grid showing several collections using Expandable

EditableInline works exactly like Expandable but adds inline editing buttons for collections that support them.

Screenshot

A property grid showing several collections using EditableInline

Collection TypeConverter

When CollectionDisplayMode property is set to Default (which is the default value), collections with continue to use their default type converters. If this property is changed to Expandable or EditableInline, then the default collection type converters will be overridden/replaced.

When using Expandable or EditableInline, the built-in data factory will check for properties that implement ICollection, IList, IDictionary, and their generic variations. When a collection property is encountered, it is represented using CollectionPropertyDescriptorPropertyModel instead of PropertyDescriptorPropertyModel.

CollectionPropertyDescriptorPropertyModel overrides the type converter of the collection, when it is one of the known default types. These types include:

  • TypeConverter
  • ArrayConverter
  • CollectionConverter
  • ReferenceConverter

If the collection uses one of these type converters, then the CollectionPropertyDescriptorPropertyModel will call its virtual CreateExpandableCollectionConverter method and will use the type converter that is returned. By default, an instance of ExpandableCollectionConverter is returned.

ExpandableCollectionConverter supports expanding any object that implements ICollection, IList, IDictionary, and their generic variations. If more control is required over the generation of the sub-items, a derivation of ExpandableCollectionConverter can be created to customize the behavior/look.

There are several PropertyGrid events that are raised before and after a child is added or removed, which are: ChildPropertyAdding, ChildPropertyAdded, ChildPropertyRemoving, and ChildPropertyRemoved.

The PropertyModelChildChangeEventArgs event arguments class used for the ChildPropertyAdding and ChildPropertyAdded events includes references to the parent IPropertyModel (the collection property), the child IPropertyModel (the item if known), and the child value being added. The ChildPropertyAdding event is raised before the item is added to the collection and the event args' Cancel property can be set to true to prevent the add. Changing the ChildValue in this event handler will also change the item being added.

The PropertyModelChildChangeEventArgs event arguments class used for the ChildPropertyRemoving and ChildPropertyRemoved events includes references to the parent IPropertyModel (the collection property), the child IPropertyModel (the item if known), and the child value being removed. The ChildPropertyRemoving event is raised before the item is removed from the collection and the event args' Cancel property can be set to true to prevent the remove.