wpf-6677-controls-and-libraries-data-grid-appearance-customization-choosing-templates-based-on-custom-logic.md
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:
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
<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>
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);
}
}
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
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:
Implement custom DataTemplates. Editors declared in these templates should follow our recommendations from this help topic: ColumnBase.CellTemplate:
Create a custom DataTemplateSelector descendant. This descendant should return templates according to your scenario requirements.
See Also