Skip to main content

DataViewBase.CellTemplate Property

Gets or sets the template used to display column cell contents. This is a dependency property.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v26.1.Core.dll

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public DataTemplate CellTemplate { get; set; }

Property Value

Type Description
DataTemplate

Defines the presentation of data cells.

Remarks

The binding source for the CellTemplate is EditGridCellData. Refer to the Data Binding section.

To apply different cell templates based on certain conditions, use a custom CellTemplateSelector. If both the CellTemplate and CellTemplateSelector are specified, the template returned by the template selector has a higher priority. If the template selector returns null, the template specified by the CellTemplate property is used.

Custom In-Place Cell Editors

We recommend that you set a custom editor in the cell template as follows:

  • The editor is a BaseEdit descendant.
  • The editor’s Name property is set to PART_Editor.

This technique has the following advantages:

  • Binds the editor’s value to the source field specified by the ColumnBase.FieldName or ColumnBase.Binding properties.
  • Full keyboard navigation.
  • Input validation support. For more information, refer to the Input Validation topic.
  • Removes the editor’s border to adjust in-place use appearance.

The code snippet below shows a basic CellTemplate with TextEdit:

<dxg:TableView.CellTemplate>
    <DataTemplate> 
        <!--DataContext is EditGridCellData-->
        <!--RowData.Row is a path to an underlying row object-->
        <dxe:TextEdit Name="PART_Editor"
            IsEnabled="{Binding RowData.Row.Enabled}" />  
        </DataTemplate>
</dxg:TableView.CellTemplate>

To customize how the GridControl binds the cell editor, use the ColumnBase.Binding property:

<dxg:TableView.CellTemplate>
    <DataTemplate>  
        <dxe:TextEdit Name="PART_Editor" IsEnabled="{Binding RowData.Row.Enabled}" />  
    </DataTemplate>
</dxg:TableView.CellTemplate>
<!---->
<dxg:GridColumn Header="Name" 
    Binding="{Binding Name, Mode=TwoWay,
        Converter={StaticResource myConverter}}" />

Non-DevExpress Editors

You can use non-BaseEdit cell editors in the CellTemplate to display and edit data. This method has the following limitations:

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

Data Binding in Detail Grid

In master-detail mode, the master row is the data context for the detail GridControl.

The View.DataContext.[YourPropertyName] binding path allows you to access the master row’s properties.

<dxg:GridControl ItemsSource="{Binding Customers}" AutoGenerateColumns="AddNew">
    <dxg:GridControl.DetailDescriptor>
        <dxg:DataControlDetailDescriptor ItemsSourcePath="Orders">
            <dxg:GridControl AutoGenerateColumns="AddNew">
                <dxg:GridColumn FieldName="LastName">
                    <dxg:GridColumn.CellTemplate>
                        <DataTemplate>
                            <dxe:TextEdit x:Name="PART_Editor" 
                                          EditValue="{Binding View.DataContext.LastName}"/>
                        </DataTemplate>
                    </dxg:GridColumn.CellTemplate>
                </dxg:GridColumn>
            </dxg:GridControl>
        </dxg:DataControlDetailDescriptor>
    </dxg:GridControl.DetailDescriptor>
</dxg:GridControl>

Data Processing and EditSettings

When you use the CellTemplate, the editor specified in the EditSettings property is ignored. However, the EditSettings value affects formatting settings, data processing (sorting, grouping, filtering, summary calculation), and export.

Events in CellTemplate

The GridControl utilizes virtualization to improve its performance. Virtualization reuses row, cell, and column visual elements, and changes their properties and data context.

Do not handle events related to changes in the EditValue, Text, Value, SelectedItem and other value-related properties to avoid issues related to virtualization. Instead, use CellValueChangingEvent and CellValueChangedEvent. The CellValueChanging event and CellValueChangingCommand work only if the template contains a BaseEdit descendant declared as described in the Custom In-Place Cell Editors section.

Refer to this KB article for more information: How to avoid problems with the DXGrid virtualization mechanism.

See Also