# Binding to a collection of PropertyDefinitions | WPF Controls | DevExpress Documentation

When engineering a WPF application using the MVVM architectural pattern, you may be required to describe [property](/WPF/15521/controls-and-libraries/property-grid/property-definitions) and [collection](/WPF/15719/controls-and-libraries/property-grid/property-definitions/collection-definitions) definitions in a Model or ViewModel. The [PropertyGridControl](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl) can automatically retrieve settings for property and collection definitions from a Model or ViewModel.

[View Example: Generate Properties From a View Model Collection](https://github.com/DevExpress-Examples/wpf-property-grid-generate-properties-from-view-model-collection)

## View Model Implementation

Assume a View Model. It includes the following classes.

- *ViewModel* - the View Model. The View Model exposes the **EditObject** that will be edited within the PropertyGrid, and a set of properties of the edit object (via the **Properties** property).
- *Employee* - a data object that contains employee information (e.g., first and last names, job position, etc.).
- *Property* - describes a property (or collection) definition at the View Model level. This class provides properties that correspond to property definition settings.

- C#

<section id="tabpanel_jRKKrTBHeZ_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code data-code-links="{&quot;/ (System)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system&quot;,&quot;/ (System.Collections.Generic)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.collections.generic&quot;,&quot;/ (System.Collections.ObjectModel)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.collections.objectmodel&quot;,&quot;/ (System.ComponentModel)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.componentmodel&quot;}" class="lang-csharp">using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication1 {
    public class ViewModel {
        public ViewModel() {
            EditObject = new Employee() {
                FirstName = &quot;John&quot;,
                LastName = &quot;Smith&quot;,
                Position = &quot;Senior Developer&quot;,
                BirthDate = new DateTime(1987, 12, 15),
                PreviousPositions = new Positions() { &quot;Junior Developer&quot;, &quot;Middle Developer&quot; }
            };
            Properties = new ObservableCollection&lt;Property&gt;();
            Properties.Add(new Property() { Name = &quot;FirstName&quot; });
            Properties.Add(new Property() { Name = &quot;LastName&quot; });
            Properties.Add(new Property() { Name = &quot;Position&quot; });
            Properties.Add(new Property() { Name = &quot;BirthDate&quot; });
            Properties.Add(new Property() { CollectionName = &quot;PreviousPositions&quot; });
        }

        public Employee EditObject { get; set; }
        public ObservableCollection&lt;Property&gt; Properties { get; set; }
    }

    public class Property {
        public string CollectionName { get; set; }
        public string Name { get; set; }
    }

    public class Employee {
        public DateTime BirthDate { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
        public Positions PreviousPositions { get; set; }
    }

    public class Positions : List&lt;string&gt; {
    }
}
</code></pre></section>

Note

If the **Properties** collection might be changed after it has been assigned to the **PropertyGrid** control, it should implement [INotifyCollectionChanged](https://learn.microsoft.com/dotnet/api/system.collections.specialized.inotifycollectionchanged), so that changes made within a View Model are automatically reflected by the property grid.

## Property Definition Templates and Template Selector

The [PropertyGridControl](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl) generates property (and collection) definitions based on property definition templates.

To choose the required template based on the property definition settings, use the *template selector*.

The code sample below demonstrates how to implement templates for property and collection definitions (see XAML: *PropertyTemplate* and *CollectionTemplate*) and how to implement a template selector (the *PropertyDefinitionTemplateSelector* class), that will select the required template for each property or collection definition.

- XAML
- C#

<section id="tabpanel_jRKKrTBHeZ-1_tabid-xaml1" role="tabpanel" data-tab="tabid-xaml1">
<pre><code class="lang-xaml">&lt;Window x:Class=&quot;WpfApplication1.MainWindow&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:dxprg=&quot;http://schemas.devexpress.com/winfx/2008/xaml/propertygrid&quot;
    xmlns:local=&quot;clr-namespace:WpfApplication1&quot;
    Height=&quot;350&quot;
    Width=&quot;525&quot;
    Title=&quot;MainWindow&quot;&gt;

    &lt;Window.DataContext&gt;
        &lt;local:ViewModel /&gt;
    &lt;/Window.DataContext&gt;

    &lt;Window.Resources&gt;
        &lt;local:PropertyDefinitionTemplateSelector x:Key=&quot;PropertyDefinitionTemplateSelector&quot;/&gt;
        &lt;DataTemplate x:Key=&quot;PropertyTemplate&quot;&gt;
            &lt;dxprg:PropertyDefinition Path=&quot;{Binding Name}&quot; /&gt;
        &lt;/DataTemplate&gt;
        &lt;DataTemplate x:Key=&quot;CollectionTemplate&quot;&gt;
            &lt;dxprg:CollectionDefinition Path=&quot;{Binding CollectionName}&quot; /&gt;
        &lt;/DataTemplate&gt;
    &lt;/Window.Resources&gt;
    &lt;Grid&gt;
        &lt;dxprg:PropertyGridControl Name=&quot;propertyGrid&quot;
            PropertyDefinitionsSource=&quot;{Binding Properties}&quot;
            PropertyDefinitionTemplateSelector=&quot;{StaticResource PropertyDefinitionTemplateSelector}&quot;
            SelectedObject=&quot;{Binding EditObject}&quot;
            ShowProperties=&quot;WithPropertyDefinitions&quot; ExpandCategoriesWhenSelectedObjectChanged=&quot;True&quot;/&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre></section>
<section id="tabpanel_jRKKrTBHeZ-1_tabid-csharp1" role="tabpanel" data-tab="tabid-csharp1" aria-hidden="true" hidden="hidden">
<pre><code data-code-links="{&quot;/ (System.Windows)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.windows&quot;,&quot;/ (System.Windows.Controls)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.windows.controls&quot;}" class="lang-csharp">using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1 {
    // A template selector.
    public class PropertyDefinitionTemplateSelector : DataTemplateSelector {
        public override DataTemplate SelectTemplate(object item, DependencyObject container) {
            Property propDef = (Property)item;
            FrameworkElement element = (FrameworkElement)container;

            if(!string.IsNullOrEmpty(propDef.CollectionName)) {
                return element.TryFindResource(&quot;CollectionTemplate&quot;) as DataTemplate;
            }
            return element.TryFindResource(&quot;PropertyTemplate&quot;) as DataTemplate;
        }
    }
    // ...
}
</code></pre></section>

Note

If the **PropertyGrid** displays property definitions that can be described using a single template, you have no need to create a template selector. Instead, assign this template to the property grid’s [PropertyGridControl.PropertyDefinitionTemplate](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl.PropertyDefinitionTemplate) property.

## Binding PropertyGrid to ViewModel

To bind the [PropertyGridControl](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl) to a ViewModel, specify the following properties.

- The [PropertyGridControl.PropertyDefinitionsSource](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl.PropertyDefinitionsSource) property - to specify the PropertyGrid’s property source.
- The [PropertyGridControl.PropertyDefinitionTemplate](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl.PropertyDefinitionTemplate) property - to specify the template used to convert objects from the PropertyGrid’s property source into property definitions.
- The [PropertyGridControl.PropertyDefinitionTemplateSelector](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl.PropertyDefinitionTemplateSelector) property - to specify the property definition template selector that returns a template for each property definition based on its type.
- The [PropertyGridControl.SelectedObject](/WPF/DevExpress.Xpf.PropertyGrid.PropertyGridControl.SelectedObject) property - to specify the edited object.

- XAML

<section id="tabpanel_jRKKrTBHeZ-2_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code class="lang-xaml">&lt;Grid&gt;
    &lt;dxprg:PropertyGridControl Name=&quot;propertyGrid&quot;
        PropertyDefinitionsSource=&quot;{Binding Properties}&quot;
        PropertyDefinitionTemplateSelector=&quot;{StaticResource PropertyDefinitionTemplateSelector}&quot;
        SelectedObject=&quot;{Binding EditObject}&quot;
        ShowProperties=&quot;WithPropertyDefinitions&quot; ExpandCategoriesWhenSelectedObjectChanged=&quot;True&quot;/&gt;
&lt;/Grid&gt;
</code></pre></section>

The image below illustrates the result.

![property_grid_mvvm.png](/WPF/images/property_grid_mvvm.png126460.png)