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

DataTemplateSelector Support

The DataTemplateSelector approach enables conditional selection of DataTemplates, based on information provided by the respective bound data item.

Read more about DataTemplateSelectors in the Template Selectors article.

DataTemplateSelectors in RadPropertyGrid

RadPropertyGrid enables its users to implement conditional editor DataTemplate selection for its PropertyGridFields trough a DataTemplateSelector:

Example 1: Creating custom DataTemplateSelector

public class Customer 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string PhoneNumber { get; set; } 
    public Department Department { get; set; } 
} 
 
public class FieldTemplateSelector : DataTemplateSelector 
{ 
    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
        PropertyDefinition propDef = item as PropertyDefinition; 
        if (propDef.DisplayName == "PhoneNumber") 
        { 
            return this.PhoneNumberDataTemplate; 
        } 
        if (propDef.SourceProperty.PropertyType == typeof(string)) 
        { 
            return this.StringDataTemplate; 
        } 
        return null; 
    } 
 
    public DataTemplate PhoneNumberDataTemplate { get; set; } 
    public DataTemplate StringDataTemplate { get; set; } 
} 

Example 2: Defining custom DataTemplateSelector

        <Grid> 
            <Grid.Resources> 
                <local:FieldTemplateSelector x:Key="DataTemplateSelector"> 
                    <local:FieldTemplateSelector.StringDataTemplate> 
                        <DataTemplate> 
                            <TextBox Foreground="Red" 
                 TextAlignment="Right" 
                 telerik:AutoBindBehavior.UpdateBindingOnElementLoaded="Text" /> 
                        </DataTemplate> 
                    </local:FieldTemplateSelector.StringDataTemplate> 
                    <local:FieldTemplateSelector.PhoneNumberDataTemplate> 
                        <DataTemplate> 
                            <telerik:RadMaskedTextInput Mask="###-###-###" Value="{Binding PhoneNumber, Mode=TwoWay}" /> 
                        </DataTemplate> 
                    </local:FieldTemplateSelector.PhoneNumberDataTemplate> 
                </local:FieldTemplateSelector> 
            </Grid.Resources> 
            <telerik:RadPropertyGrid x:Name="rpg" EditorTemplateSelector="{StaticResource DataTemplateSelector}" /> 
        </Grid> 

You might wonder where these "telerik" and "local", in front of the tags RadPropertyGrid and FieldTemplateSelector, came from. Well, these are the names of the namespaces you give when you import the appropriate assembly or your custom DataTemplateSelector class into the XAML file.

Rad Property Grid Template Selectors

The AutoBind attached behavior is fully compatible with the DataTemplateSelector approach.

In this article