Version

Configuring Data Template Selector for Editor Definitions (xamPropertyGrid)

Purpose

This topic explains how create custom logic for assigning editing template on an editor definition.

Required Background

Topic Purpose

This topic explains the features supported by the control from developer perspective.

This topic provides an overview of the visual elements of the control.

This topic explains how to customize the editors used for editing the properties’ values.

Introduction

In some cases it may not be enough to specify a custom editor depending on its type, category or name. As an alternative approach you may use the editor definition’s TemplateSelector property and provide an object which extends from the DataTemplateSelector class. When a template selector is provided to an editor definition the EditTemplate property and the ReadOnlyTemplate property are ignored and the resolved template will be the one returned by the template selector’s SelectTemplate method. In addition, the template selector has access to the edit template and the read-only template and can either return one of them or return any other data template based on its internal logic.

Providing Template Selector

Your custom template selector must extend from the DataTemplateSelector class and implements the SelectTemplate method. This method has the following arguments:

Having access to the xamPropertyGrid control and the property item for which the control is requesting a template should be sufficient for you to provide an adequate template. In addition you have access to the editing and read-only templates specified on the editor definition. Your implementation can either return any of them or a data template based on your custom logic.

If the template selector returns null the xamPropertyGrid will use the default built-in editor for the property.

Template Selector Example

The following example shows how to create two data templates. The first data template (with key "SpinIntEditor") will be used for all properties of type Int32. The second data template (with key "SliderIntEditor") will be used by the Template Selector and will be returned and set only on the property with name "Level".

In XAML:

<Page
  …
  xmlns:ig="http://schemas.infragistics.com/xaml"
  …>
  <Page.Resources>
    <ResourceDictionary>
      <DataTemplate x:Key="SpinIntEditor">
        <igEditors:XamNumericEditor Value="{Binding Path=Value}" ValueType="{x:Type sys:Int32}"
          SpinButtonDisplayMode="Always" Theme="IGTheme" />
      </DataTemplate>
      <DataTemplate x:Key="SliderIntEditor">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>
          <TextBlock Grid.Column="0" MinWidth="20" Margin="2"
            Text="{Binding Path=Value, Converter={StaticResource IntDoubleConv}, Mode=OneWay}" />
          <Slider Grid.Column="1" Minimum="0" Maximum="5" TickFrequency="1" IsSnapToTickEnabled="True"
            Value="{Binding Path=Value, Converter={StaticResource IntDoubleConv}}" />
        </Grid>
      </DataTemplate>
      <local:CustomDataTemplateSelector x:Key="CustomSelector" />
    </ResourceDictionary>
  </Page.Resources>
  <Grid>
    <ig:XamPropertyGrid>
      <ig:XamPropertyGrid.EditorDefinitions>
        <ig:PropertyGridEditorDefinition
          TargetType="{x:Type sys:Int32}"
          EditTemplate="{StaticResource SpinIntEditor}"
          TemplateSelector="{StaticResource CustomSelector}">
        </ig:PropertyGridEditorDefinition>
      </ig:XamPropertyGrid.EditorDefinitions>
    </ig:XamPropertyGrid>
  </Grid>
</Page>

The following code example shows the implementation of the Template Selector which assigns certain data template based on a property name. This functionality will override the template which will be set by the control based on a property type.

In Visual Basic:

Public Class CustomDataTemplateSelector
  Inherits DataTemplateSelector
  Public Overrides Function SelectTemplate(item As Object, container _
 As DependencyObject) As DataTemplate
    Dim context As PropertyGridTemplateSelectorContext = _
      TryCast(item, PropertyGridTemplateSelectorContext)
    If context.PropertyItem.PropertyName.Equals("Level") Then
      Dim propertyGrid As XamPropertyGrid = TryCast(container, XamPropertyGrid)
      Dim resource = propertyGrid.TryFindResource("SliderIntEditor")
      Return DirectCast(resource, DataTemplate)
    End If
    Return context.EditTemplate
  End Function
End Class

In C#:

public class CustomDataTemplateSelector : DataTemplateSelector
{
  public override DataTemplate SelectTemplate(object item, DependencyObject container)
  {
    PropertyGridTemplateSelectorContext context = item as PropertyGridTemplateSelectorContext;
    if (context.PropertyItem.PropertyName.Equals("Level"))
    {
      XamPropertyGrid propertyGrid = container as XamPropertyGrid;
      var resource = propertyGrid.TryFindResource("SliderIntEditor");
      return (DataTemplate)resource;
    }
    return context.EditTemplate;
  }
}

Re-evaluate the Template Selector Programmatically

There are cases when you may need to re-evaluate the template selector’s value programmatically. For example if your template selector’s code is returning different data templates based on the value of another property you can initiate re-evaluation by invoking the PropertyGridPropertyItem's RequeryEditorDefinition method.

Note
Note

You can be notified when a property’s value has changed by attaching an event handler to the xamPropertyGrid’s PropertyItemValueChanged event and to find the PropertyGridPropertyItem associated with a specific property you can call the existing xamPropertyGrid’s FindPropertyItem method.

Related Topics

Topic Purpose

This topic explains how to configure some general control options.

This topic explains how to configure the filtering of the control’s properties list.

This topic explains how to provide logic for custom sorting of the properties list.