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: Applying Templates Based on Custom Logic
This example demonstrates how to use DataRowTemplateSelector to apply different templates to even and odd data rows.
Use the How to dynamically select a row details template with DataTemplateSelector example if you want to dynamically select Row Details Templates.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows
Namespace DXGrid_TemplateSelector
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
grid.ItemsSource = IssueDataObject.GetData()
End Sub
End Class
Public Class IssueDataObject
Public Property IssueName() As String
Public Shared Function GetData() As List(Of IssueDataObject)
Dim data As New List(Of IssueDataObject)()
data.Add(New IssueDataObject() With {.IssueName = "Transaction History"})
data.Add(New IssueDataObject() With {.IssueName = "Ledger: Inconsistency"})
data.Add(New IssueDataObject() With {.IssueName = "Data Import"})
data.Add(New IssueDataObject() With {.IssueName = "Data Archiving"})
Return data
End Function
End Class
End Namespace
Imports DevExpress.Xpf.Grid
Imports System
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls
Namespace DXGrid_TemplateSelector
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
End Namespace
<Window x:Class="DXGrid_TemplateSelector.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:local="clr-namespace:DXGrid_TemplateSelector"
Width="300"
Height="300"
Title="Window1">
<Window.Resources>
<DataTemplate x:Key="evenRowTemplate">
<Border Margin="1"
Background="Blue"
CornerRadius="5">
<TextBlock Margin="5"
Foreground="White"
Text="{Binding DataContext.IssueName}" />
</Border>
</DataTemplate>
<DataTemplate x:Key="oddRowTemplate">
<Border Margin="1"
Background="Orange"
CornerRadius="5">
<TextBlock Margin="5"
Foreground="White"
Text="{Binding DataContext.IssueName}" />
</Border>
</DataTemplate>
<local:RowTemplateSelector x:Key="rowTemplateSelector"
EvenRowTemplate="{StaticResource evenRowTemplate}"
OddRowTemplate="{StaticResource oddRowTemplate}" />
</Window.Resources>
<Grid>
<dxg:GridControl x:Name="grid"
AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True"
DataRowTemplateSelector="{StaticResource rowTemplateSelector}"
NavigationStyle="None"
ShowColumnHeaders="False"
ShowGroupPanel="False"
ShowHorizontalLines="False" />
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace DXGrid_TemplateSelector {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
grid.ItemsSource = IssueDataObject.GetData();
}
}
public class IssueDataObject {
public string IssueName { get; set; }
public static List<IssueDataObject> GetData() {
List<IssueDataObject> data = new List<IssueDataObject>();
data.Add(new IssueDataObject() { IssueName = "Transaction History" });
data.Add(new IssueDataObject() { IssueName = "Ledger: Inconsistency" });
data.Add(new IssueDataObject() { IssueName = "Data Import" });
data.Add(new IssueDataObject() { IssueName = "Data Archiving" });
return data;
}
}
}
using DevExpress.Xpf.Grid;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace DXGrid_TemplateSelector
{
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);
}
}
}
Example 2: How to Change a Cell Template based on Custom Logic