New to Telerik UI for WPF? Download free 30-day trial

Set Generic ItemTemplate Property of CollectionEditorPicker


Product RadPropertyGrid for WPF


How to set a generic ItemTemplate property for all CollectionEditorPicker controls inside your RadPropertyGrid.


First, we can create a custom attached property which will be set to the CollectionEditorPicker control. Inside the property changed method, we can subscribe to the Loaded event of the control and sets its ItemTemplate property.

Example 1: Create Models

public class MyCollectionEditorPickerBehavior 
    public static bool GetIsEnabled(DependencyObject obj) 
        return (bool)obj.GetValue(IsEnabledProperty); 
    public static void SetIsEnabled(DependencyObject obj, bool value) 
        obj.SetValue(IsEnabledProperty, value); 
    public static readonly DependencyProperty IsEnabledProperty = 
        DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(MyCollectionEditorPickerBehavior), new PropertyMetadata(false, OnIsEnabledChanged)); 
    private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        var collectionEditor = d as CollectionEditorPicker; 
        collectionEditor.Loaded += (s, a) => 
            var window = collectionEditor.ParentOfType<Window>(); 
            collectionEditor.CollectionEditor.ItemTemplate = window.Resources["ItemTemplate"] as DataTemplate; 

The next step is to set the attached property to the CollectionEditorPicker. To do that we can subscribe to the AutoGeneratingPropertyDefinition event of the RadPropertyGrid. In its event handler, we can create DataTemplate in code and assign it to the e.PropertyDefinition.EditorTemplate from the event arguments. The DataTemplate will include CollectionEditorPicker with the mentioned above custom attached property.

Example 2:

private void RadPropertyGrid_AutoGeneratingPropertyDefinition(object sender, Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs e) 
        // Your custom collection properties 
    if (e.PropertyDefinition.DisplayName == "Players" && e.PropertyDefinition.DisplayName == "Coaches") 
        e.PropertyDefinition.Binding = new Binding("Name"); 
        var dataTemplateString = @"<DataTemplate>" + 
        @"<telerik:CollectionEditorPicker local:MyCollectionEditorPickerBehavior.IsEnabled=""True"" Source=""{Binding " + e.PropertyDefinition.DisplayName + @"}"" Header=""" + e.PropertyDefinition.DisplayName + @""" Grid.Row=""1"" />" + 
        ParserContext parserContext = new ParserContext(); 
        parserContext.XmlnsDictionary.Add("", ""); 
        parserContext.XmlnsDictionary.Add("telerik", ""); 
        parserContext.XmlnsDictionary.Add("local", "clr-namespace:StringListEditor;assembly=StringListEditor"); 
        DataTemplate template = (DataTemplate)XamlReader.Parse(dataTemplateString, parserContext) 
        e.PropertyDefinition.EditorTemplate = template; 

See Also

In this article
Not finding the help you need? Improve this article