Skip to main content

DXDataTemplateSelector

The DXDataTemplateSelector is a declarative way to implement DataTemplateSelector.

The DXDataTemplateSelector allows you to define a simple template selection logic in XAML, so you do not need to create a DataTemplateSelector ancestor in code-behind. The DXDataTemplateSelector works like WPF triggers.

The code sample below illustrates how to select different data templates for different ListBox items using the DXDataTemplateSelector.

<DXDataTemplateSelector x:Key="myDataTemplateSelector">
    <DXDataTemplateTrigger Binding="{Binding Priority}" Value="1" Template="{StaticResource importantTaskTemplate}"/>
    <DXDataTemplateTrigger Template="{StaticResource myTaskTemplate}"/>
</DXDataTemplateSelector>
...
<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}"
         ItemTemplateSelector="{StaticResource myDataTemplateSelector}"
         HorizontalContentAlignment="Stretch"/>

Specifics

  • The DXDataTemplateTrigger requires specifying both the DXTriggerBase.Binding and DXTriggerBase.Value properties to return a data template that satisfies the condition (that is, the specified property is set to a specified value). When both the Property and Value properties are not specified, the DXDataTemplateTrigger serves as the default template source.
  • You can assign a template to DXDataTemplateTrigger explicitly or as a static resource only. Dynamic resources are not supported.
  • The last positive trigger provides the template (like regular WPF triggers).
  • The DXDataTemplateSelector becomes immutable (you cannot modify a trigger collection as well as change trigger properties) once its template selection logic is called.