CellTemplate
razor
[Parameter] public RenderFragment<T>? CellTemplate { get; set; }
Reference to an item currently being rendered can be accessed via
@context
variable.
Examples
The following example demonstrates how an item of type
Product
(specified by typeparam T
) can be accessed within single cell template, while the cell is being rendered.
razor
<DataGrid T="Product" ItemsSource="ProductsList">
<GridColumn HeaderText="Price" Size="60px">
<CellTemplate>
@context.Price <em>USD</em>
</CellTemplate>
</GridColumn>
</DataGrid>
Even a more complex code can be placed within the template. The following example also uses context object.
razor
<DataGrid T="Product" ItemsSource="ProductsList"> <GridColumn HeaderText="Actions" Size="60px"> <CellTemplate> <Toolbar> <ToolButton OnClick="e => HandleDeleteClick(context)"> Delete </ToolButton> </Toolbar> </CellTemplate> </GridColumn> </DataGrid> @code { private void HandleDeleteClick(Product product) { Products.Remove(product); } }