Back to Devexpress

ColumnBase.CellTemplateSelector Property

wpf-devexpress-dot-xpf-dot-grid-dot-columnbase-40ac602b.md

latest5.3 KB
Original Source

ColumnBase.CellTemplateSelector Property

Gets or sets an object that chooses a cell template based on custom logic. This is a dependency property.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.Core.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public DataTemplateSelector CellTemplateSelector { get; set; }
vb
Public Property CellTemplateSelector As DataTemplateSelector

Property Value

TypeDescription
DataTemplateSelector

A DataTemplateSelector descendant that chooses a template based on custom logic.

|

Remarks

The ColumnBase.CellTemplate property specifies the template that defines the contents of a column cell. If you have more than one template to render cells, you can implement custom logic to choose the required template. To do this, derive from the DataTemplateSelector class and implement the SelectTemplate method that returns a template for each condition.

If you specify both the ColumnBase.CellTemplate and CellTemplateSelector , the CellTemplateSelector defines the cell template. If the template selector returns null , the template specified by the ColumnBase.CellTemplate property is used.

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

View Example: How to change a cell template based on custom logic

To implement this task, 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.

Data Binding

Cell elements contain EditGridCellData objects in their DataContext.

Use the following binding paths to access cell values, columns, and ViewModel properties:

  • Value - access the current cell value;
  • Column - access the current column;
  • RowData.Row.[YourPropertyName] - access a property of an object from the ItemsSource collection;
  • Data.[FieldName] - access column values in Server Mode or if you use the RealTimeSource, access unbound column values;
  • View.DataContext.[YourPropertyName] - access a property in a grid’s ViewModel.

View Example: Build Binding Paths in WPF Data Grid Cells

The following code snippets (auto-collected from DevExpress Examples) contain references to the CellTemplateSelector property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-data-grid-change-cell-template-based-on-custom-logic/CS/Window1.xaml#L43

xml
<dxg:GridColumn FieldName="Value">
    <dxg:GridColumn.CellTemplateSelector>
        <local:EditorTemplateSelector />

wpf-data-grid-filter-columns-bound-to-collection-properties/CS/FilterDropDown_AgregateOperators/MainWindow.xaml#L26

xml
<Setter Property="Width" Value="*"/>
<Setter Property="CellTemplateSelector">
    <Setter.Value>

See Also

Choosing Templates Based on Custom Logic

ColumnBase Class

ColumnBase Members

DevExpress.Xpf.Grid Namespace