Partilhar via


DataGridTemplateColumn.CellTemplate Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the template that is used to display the contents of a cell that is not in editing mode.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Data (in System.Windows.Controls.Data.dll)

Syntax

'Declaration
Public Property CellTemplate As DataTemplate
public DataTemplate CellTemplate { get; set; }
<sdk:DataGridTemplateColumn>
  <sdk:DataGridTemplateColumn.CellTemplate>
    dataTemplate
  </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CellTemplate="templateReference"/>

XAML Values

Both attribute and property element syntax for RowDetailsTemplate are shown because defining the template inline as opposed to referencing an existing one as a resource are both equally valid scenarios.

Property Value

Type: System.Windows.DataTemplate
The template that is used to display the contents of a cell that is not in editing mode. The default is nulla null reference (Nothing in Visual Basic).

Examples

The following code example demonstrates how to set the CellTemplate property in XAML. This example is part of a larger example available in the DataGrid class overview.

<sdk:DataGrid x:Name="dataGrid5" 
    Height="125" Margin="0,5,0,10"
    AutoGenerateColumns="False"
    RowBackground="Azure"
    AlternatingRowBackground="LightSteelBlue">
    <sdk:DataGrid.Columns>
        <!-- Name Column -->
        <sdk:DataGridTemplateColumn Header="Name">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <TextBlock Padding="5,0,5,0"
                            Text="{Binding FirstName}"/>
                        <TextBlock Text="{Binding LastName}"/>
                    </StackPanel>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate> 
            <sdk:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBox Text="{Binding FirstName}" BorderThickness="0"/>
                        <TextBox Text="{Binding LastName}" BorderThickness="0"/>
                    </StackPanel>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellEditingTemplate> 
        </sdk:DataGridTemplateColumn>              
        <!-- Address Column -->
        <sdk:DataGridTextColumn
            Header="Address" Width="300"
            Binding="{Binding Address}" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.