Back to Devexpress

Choosing Templates Based on Custom Logic

wpf-6677-controls-and-libraries-data-grid-appearance-customization-choosing-templates-based-on-custom-logic.md

latest4.9 KB
Original Source

Choosing Templates Based on Custom Logic

  • Apr 14, 2025
  • 3 minutes to read

Columns and Views provide multiple properties that allow you to define templates and change the visual presentation of their elements (cells, rows, summary items, etc.). A template is applied to multiple elements in the same scope. If however, you have more than one template that can be applied to a target element (e.g. a cell, row), you can implement custom logic to choose the required template. This allows you to provide a different visual appearance for individual grid elements.

For example, a template that defines the presentation of data rows is specified by the TableView.DataRowTemplate property. If you want to conditionally apply templates to them:

  • Create a template selector - a class that chooses a template if the required condition is met. This class must derive from the DataTemplateSelector class and override the SelectTemplate method, to return a template which meets the required condition.
  • Assign its instance to the TableView.DataRowTemplateSelector property.

Example 1: Apply Row Templates Based on Custom Logic

This example demonstrates how to use DataRowTemplateSelector to apply different templates to even and odd data rows.

View Example: Select a Row Template Based on Custom Logic

xaml
<Window.Resources>
    <DataTemplate x:Key="evenRowTemplate">
        <Border Margin="1"
                Background="Blue"
                CornerRadius="5">
            <TextBlock Margin="5"
                       Foreground="White"
                       Text="{Binding Row.IssueName}"/>
        </Border>
    </DataTemplate>
    <DataTemplate x:Key="oddRowTemplate">
        <Border Margin="1"
                Background="Orange"
                CornerRadius="5">
            <TextBlock Margin="5"
                       Foreground="White"
                       Text="{Binding Row.IssueName}"/>
        </Border>
    </DataTemplate>
    <local:RowTemplateSelector x:Key="rowTemplateSelector"
                               EvenRowTemplate="{StaticResource evenRowTemplate}"
                               OddRowTemplate="{StaticResource oddRowTemplate}"/>
</Window.Resources>
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True" DataRowTemplateSelector="{StaticResource rowTemplateSelector}"/>
    </dxg:GridControl.View>
</dxg:GridControl>
cs
public class RowTemplateSelector : DataTemplateSelector {
    public DataTemplate EvenRowTemplate { get; set; }
    public DataTemplate OddRowTemplate { get; set; }
    public override DataTemplate SelectTemplate(object item, DependencyObject container) {
        RowData row = item as RowData;
        if (row != null)
            return row.EvenRow ? EvenRowTemplate : OddRowTemplate;
        return base.SelectTemplate(item, container);
    }
}
vb
Public Class RowTemplateSelector
    Inherits DataTemplateSelector

    Public Property EvenRowTemplate() As DataTemplate
    Public Property OddRowTemplate() As DataTemplate
    Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
        Dim row As RowData = TryCast(item, RowData)
        If row IsNot Nothing Then
            Return If(row.EvenRow, EvenRowTemplate, OddRowTemplate)
        End If
        Return MyBase.SelectTemplate(item, container)
    End Function
End Class

Example 2: Change a Cell Template Based on Custom Logic

This sample illustrates how to use the CellTemplateSelector to change a cell template based on a condition:

View Example: Change a Cell Template Based on Custom Logic

To implement this approach, do the following:

  1. Implement custom DataTemplates. Editors declared in these templates should follow our recommendations from this help topic: ColumnBase.CellTemplate:

  2. Create a custom DataTemplateSelector descendant. This descendant should return templates according to your scenario requirements.

See Also

Select a Row Details Template Based on Custom Logic