Skip to main content
A newer version of this page is available. .

Choosing Templates Based on Custom Logic

  • 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: 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.

View Example

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

Example 2: How to Change a Cell Template based on Custom Logic

View Example: How to Change a Cell Template based on Custom Logic