Skip to content

DevExpress-Examples/wpf-propertygrid-add-an-item-to-a-collection-or-a-dictionary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WPF PropertyGrid - Add an Item to a Collection or a Dictionary

This example displays a custom collection/dictionary property in the DevExpress WPF Property Grid and allows users to add new items/entries as requirements/business needs dictate.

The following image illustrates available user actions:

Add an item to a collection

To incorporate this feature in your DevExpress-powered WPF app (allow users to add new items to a collection/dictionary), set one of the following properties to true:

<dxprg:CollectionDefinition UseCollectionEditor="True" >

Implementation Details

The project includes the following examples:

  • Collection - An example with a collection property.
  • Dictionary - An example with a dictionary property.

The Collection Example

Use the XamlInitializer class to allow users to add an item to a collection.

  1. Add the XamlInitializer class to the Window.Resources section.
    <Window.Resources>
        <dxprg:XamlInitializer Initialize="XamlInitializer_Initialize" x:Key="xamlInitializer" />
    </Window.Resources>
  2. Specify a TypeDefinition and its attributes.
    <dxprg:XamlInitializer Initialize="XamlInitializer_Initialize" x:Key="xamlInitializer">
        <dxprg:TypeDefinition Type="{x:Type local:Supplier}"  Name="Supplier" Description="New Supplier"/>
    </dxprg:XamlInitializer>
  3. In the XamlInitializer.Initialize event handler, initialize the new object's properties.
    private void XamlInitializer_Initialize(object sender, InstanceInitializeEventArgs e) {
        e.Instance.FirstName = "FIRSTNAME";
        e.Instance.LastName = "LASTNAME";
        e.Instance.Phone = "PHONE";
    }

The Dictionary Example

Use Property attributes to allow users to add items to a dictionary.

In this example, the DictionaryKey1Attribute class creates a unique key for a new item. Use this example as a template and create your own attribute class to generate unique keys.

  1. Add a DictionaryKey1Attribute class inherited from the DevExpress.Mvvm.DataAnnotations.NewItemInstanceInitializerAttribute class.
    public class DictionaryKey1Attribute : NewItemInstanceInitializerAttribute {
        public DictionaryKey1Attribute() : base(typeof(Supplier)) { }
        public override KeyValuePair<object, object>? CreateInstance(ITypeDescriptorContext context, IEnumerable dictionary) {
            var testObject = ((DescriptorContext)context).Value as IDictionary<string, Supplier>;
            int key = testObject.Keys.Count;
            while(testObject.Keys.Contains(key.ToString()))
                key++;
            return new KeyValuePair<object, object>(key.ToString(), new Supplier());
        }
    }
  2. Add the attribute to the dictionary property.
    [DictionaryKey1()]
    public IDictionary<string, Supplier> Tags { get; set; } = new Dictionary<string, Supplier>();

Documentation

Files to Review

Collections example:

Dictionary example:

About

This example displays a custom collection/dictionary property with the DevExpress WPF Property Grid and allows users to add new items/entries as needs dictate.

Topics

Resources

License

Stars

Watchers

Forks