Skip to main content
A newer version of this page is available. .

Collection Definitions

  • 5 minutes to read

Overview

Collection definitions represent the collection properties of a bound object and specify their appearance within the property grid. Each collection definition is linked to one or multiple collection properties of a bound object. Collection definitions are represented by the CollectionDefinition objects that are stored within the PropertyGridControl.PropertyDefinitions collection.

Collection Definition 1

Linking Collection Properties to Definitions

You can link a collection property of a bound object to a collection definition using an association criteria. Different criteria and combinations of criteria have different priorities. When an object property matches multiple definitions, the PropertyGrid control links it to the definition that has the highest priority.

Criteria

Priority

Description

Collection property path

High

Use the PropertyDefinitionBase.Path property to specify the direct path to the associated property.

Parent property path

Medium

Use the PropertyDefinition.Scope property to specify the path to the parent property.

Collection property type

Low

Use the PropertyDefinition.Type property to specify the type of the associated collection property.

The PropertyDefinition.TypeMatchMode property specifies the rules of type matching. See TypeMatchMode to learn more.

Note

Collection definitions generally have higher priority than property definitions when linking to collection properties, except when a property definition specifies the path to the collection property.

The following example demonstrates a declaration of a collection definition.

<dxprg:CollectionDefinition Path="Suppliers">
    <dxprg:PropertyDefinition Path="*.Name"/>
    <dxprg:PropertyDefinition Path="*.Phone"/>
</dxprg:CollectionDefinition>

Displaying Collections Within the PropertyGrid Control

If the PropertyGridControl.ShowProperties property is set to WithPropertyDefinitions, you need to initialize the properties of the collection items manually. To hide the properties of collection items within a property grid, set the PropertyDefinitionBase.ShowChildren (using CollectionDefinition.ShowChildren) property to false.

The following example demonstrates a property grid that is configured in the following way.

  • The property grid displays all public properties of the bound object.
  • Only string properties are displayed within the collection items.
<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
...
<dxprg:PropertyGridControl SelectedObject="{Binding DemoProduct}" ShowProperties="WithPropertyDefinitions">

    <dxprg:CollectionDefinition Path="Suppliers">
        <!--Associated with the String properties of Supplier objects-->
        <dxprg:PropertyDefinition Type="sys:String"/>
    </dxprg:CollectionDefinition>

    <!--Links to all public properties of the Product object-->
    <dxprg:PropertyDefinition Path="*"/>

</dxprg:PropertyGridControl>

Collection Definition 1

Collection Editing

The PropertyGrid control features the built-in collection editor. The collection editor allows end-users to make the following operations.

  • Edit collection items.
  • Add items to the collection.
  • Remove items from the collection.

PG_CollectionEditorGIF

The following properties control the availability of the collection editor.

Property name Property description
PropertyGridControl.UseCollectionEditor Toggles collection editing for the entire property grid.
CollectionDefinition.UseCollectionEditor Toggles collection editing for individual collections.

New Item Initializers

You can use the item initializers to create new collection items. A new item initializer is an object that implements the DevExpress.Mvvm.Native.IInstanceInitializer interface.

New item initializers allow you to do the following.

  • Specify the default property values.
  • Create objects without the default constructor.
  • Configure the new item menu. The new item menu allows end-users to add objects of different types to a collection.

The following example demonstrates a PropertyGrid control bound to a Supplier object. The Supplier.Products collection property stores objects of various types. The new item initializer allows end-users to choose the type of the object that is added to the collection.

<Window.Resources>
    <ResourceDictionary>
        <local:ProductItemInitializer x:Key="productItemInitializer" />
    </ResourceDictionary>
</Window.Resources>
<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
...
<dxprg:PropertyGridControl SelectedObject="{Binding DemoSupplier}" ShowProperties="WithPropertyDefinitions">
    <dxprg:CollectionDefinition Path="Products" AllowNewItemInitializer="True" NewItemInitializer="{StaticResource productItemInitializer}"> 
        <dxprg:PropertyDefinition Scope="*"/>
    </dxprg:CollectionDefinition>
    <dxprg:PropertyDefinition/>
</dxprg:PropertyGridControl>

PropertyGrid ItemInitializer

See Also