2 out of 3 rated this helpful - Rate this topic

DataGridTemplateColumn Class

Represents a DataGrid column that hosts template-specified content in its cells.

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Controls.DataGridColumn
        System.Windows.Controls.DataGridTemplateColumn

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
public class DataGridTemplateColumn : DataGridColumn
<DataGridTemplateColumn .../>

The DataGridTemplateColumn type exposes the following members.

  Name Description
Public method DataGridTemplateColumn Initializes a new instance of the DataGridTemplateColumn class.
Top
  Name Description
Public property ActualWidth Gets the current width of the column, in device-independent units (1/96th inch per unit). (Inherited from DataGridColumn.)
Public property CanUserReorder Gets or sets a value that indicates whether the user can change the column display position by dragging the column header. (Inherited from DataGridColumn.)
Public property CanUserResize Gets or sets a value that indicates whether the user can adjust the column width by using the mouse. (Inherited from DataGridColumn.)
Public property CanUserSort Gets or sets a value that indicates whether the user can sort the column by clicking the column header. (Inherited from DataGridColumn.)
Public property CellEditingTemplate Gets or sets the template to use to display the contents of a cell that is in editing mode.
Public property CellEditingTemplateSelector Gets or sets the object that determines which template to use to display the contents of a cell that is in editing mode.
Public property CellStyle Gets or sets the style that is used to render cells in the column. (Inherited from DataGridColumn.)
Public property CellTemplate Gets or sets the template to use to display the contents of a cell that is not in editing mode.
Public property CellTemplateSelector Gets or sets the object that determines which template to use to display the contents of a cell that is not in editing mode.
Public property ClipboardContentBinding Gets or sets the binding object to use when getting or setting cell content for the clipboard. (Inherited from DataGridColumn.)
Protected property DataGridOwner Gets the DataGrid control that contains this column. (Inherited from DataGridColumn.)
Public property DependencyObjectType Gets the DependencyObjectType that wraps the CLR type of this instance.  (Inherited from DependencyObject.)
Public property Dispatcher Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public property DisplayIndex Gets or sets the display position of the column relative to the other columns in the DataGrid. (Inherited from DataGridColumn.)
Public property DragIndicatorStyle Gets or sets the style object to apply to the column header during a drag operation. (Inherited from DataGridColumn.)
Public property Header Gets or sets the content of the column header. (Inherited from DataGridColumn.)
Public property HeaderStringFormat Gets or sets the format pattern to apply to the content of the column header. (Inherited from DataGridColumn.)
Public property HeaderStyle Gets or sets the style that is used when rendering the column header. (Inherited from DataGridColumn.)
Public property HeaderTemplate Gets or sets the template that defines the visual representation of the column header. (Inherited from DataGridColumn.)
Public property HeaderTemplateSelector Gets or sets the object that selects which template to use for the column header. (Inherited from DataGridColumn.)
Public property IsAutoGenerated Gets a value that indicates whether the column is auto-generated. (Inherited from DataGridColumn.)
Public property IsFrozen Gets a value that indicates whether the column is prevented from scrolling horizontally. (Inherited from DataGridColumn.)
Public property IsReadOnly Gets or sets a value that indicates whether cells in the column can be edited. (Inherited from DataGridColumn.)
Public property IsSealed Gets a value that indicates whether this instance is currently sealed (read-only). (Inherited from DependencyObject.)
Public property MaxWidth Gets or sets the maximum width constraint of the column. (Inherited from DataGridColumn.)
Public property MinWidth Gets or sets the minimum width constraint of the column. (Inherited from DataGridColumn.)
Public property SortDirection Gets or sets the sort direction (ascending or descending) of the column. (Inherited from DataGridColumn.)
Public property SortMemberPath Gets or sets a property name, or a period-delimited hierarchy of property names, that indicates the member to sort by. (Inherited from DataGridColumn.)
Public property Visibility Gets or sets the visibility of the column. (Inherited from DataGridColumn.)
Public property Width Gets or sets the column width or automatic sizing mode. (Inherited from DataGridColumn.)
Top
  Name Description
Protected method CancelCellEdit Causes the cell being edited to revert to the original, unedited value. (Inherited from DataGridColumn.)
Public method CheckAccess Determines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Public method ClearValue(DependencyProperty) Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier. (Inherited from DependencyObject.)
Public method ClearValue(DependencyPropertyKey) Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey. (Inherited from DependencyObject.)
Public method CoerceValue Coerces the value of the specified dependency property. This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject. (Inherited from DependencyObject.)
Protected method CommitCellEdit Performs any required validation before exiting cell editing mode. (Inherited from DataGridColumn.)
Public method Equals Determines whether a provided DependencyObject is equivalent to the current DependencyObject. (Inherited from DependencyObject.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method GenerateEditingElement Gets an element defined by the CellEditingTemplate that is bound to the column's Binding property value. (Overrides DataGridColumn.GenerateEditingElement(DataGridCell, Object).)
Protected method GenerateElement Gets an element defined by the CellTemplate that is bound to the column's Binding property value. (Overrides DataGridColumn.GenerateElement(DataGridCell, Object).)
Public method GetCellContent(DataGridRow) Retrieves the Content property value for the cell at the intersection of this column and the specified row. (Inherited from DataGridColumn.)
Public method GetCellContent(Object) Gets the Content property value for the cell at the intersection of this column and the row that represents the specified data item. (Inherited from DataGridColumn.)
Public method GetHashCode Gets a hash code for this DependencyObject. (Inherited from DependencyObject.)
Public method GetLocalValueEnumerator Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject. (Inherited from DependencyObject.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue Returns the current effective value of a dependency property on this instance of a DependencyObject. (Inherited from DependencyObject.)
Public method InvalidateProperty Re-evaluates the effective value for the specified dependency property (Inherited from DependencyObject.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method NotifyPropertyChanged Notifies the DataGrid that contains this column that a column property has changed. (Inherited from DataGridColumn.)
Protected method OnCoerceIsReadOnly Determines the value of the IsReadOnly property based on the property rules of the DataGrid that contains this column. (Inherited from DataGridColumn.)
Public method OnCopyingCellClipboardContent Raises the CopyingCellClipboardContent event. (Inherited from DataGridColumn.)
Public method OnPastingCellClipboardContent Raises the PastingCellClipboardContent event. (Inherited from DataGridColumn.)
Protected method OnPropertyChanged Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. The specific dependency property that changed is reported in the event data. (Inherited from DependencyObject.)
Protected method PrepareCellForEdit When overridden in a derived class, sets cell content as needed for editing. (Inherited from DataGridColumn.)
Public method ReadLocalValue Returns the local value of a dependency property, if it exists. (Inherited from DependencyObject.)
Protected method RefreshCellContent Refreshes the contents of a cell in the column in response to a template property value change. (Overrides DataGridColumn.RefreshCellContent(FrameworkElement, String).)
Public method SetCurrentValue Sets the value of a dependency property without changing its value source. (Inherited from DependencyObject.)
Public method SetValue(DependencyProperty, Object) Sets the local value of a dependency property, specified by its dependency property identifier. (Inherited from DependencyObject.)
Public method SetValue(DependencyPropertyKey, Object) Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property. (Inherited from DependencyObject.)
Protected method ShouldSerializeProperty Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property. (Inherited from DependencyObject.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method VerifyAccess Enforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
  Name Description
Public event CopyingCellClipboardContent Occurs after the cell clipboard content is prepared. (Inherited from DataGridColumn.)
Public event PastingCellClipboardContent Occurs before the clipboard content is moved to the cell. (Inherited from DataGridColumn.)
Top
  Name Description
Public field Static member CellEditingTemplateProperty Identifies the CellEditingTemplate dependency property.
Public field Static member CellEditingTemplateSelectorProperty Identifies the CellEditingTemplateSelector dependency property.
Public field Static member CellTemplateProperty Identifies the CellTemplate dependency property.
Public field Static member CellTemplateSelectorProperty Identifies the CellEditingTemplateSelector dependency property.
Top

The DataGridTemplateColumn type enables you to create your own column types by specifying the cell templates to use when displaying and editing values. To specify the template that is used to display the contents of a cell that is not in editing mode, set the CellTemplate property. To specify the template that is used to display the contents of a cell that is in editing mode, set the CellEditingTemplate property. For more information about templates, see Data Templating Overview and Styling and Templating.

For convenience, DataGrid provides the following pre-defined column types:

Column Type

Data Display

DataGridHyperlinkColumn

Use to display URI data.

DataGridComboBoxColumn

Use to display enumeration data.

DataGridTextColumn

Use to display text.

DataGridCheckBoxColumn

Use to display Boolean data.

The following example shows how to create a DataGridTemplateColumn.


<Grid>
    <Grid.Resources>
        <!--DataTemplate for Published Date column defined in Grid.Resources.  PublishDate is a property on the ItemsSource of type DateTime -->
        <DataTemplate x:Key="DateTemplate" >
            <StackPanel Width="20" Height="30">
                <Border Background="LightBlue" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" />
                </Border>
                <Border Background="White" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" />
                </Border>
            </StackPanel>
        </DataTemplate>
        <!--DataTemplate for the Published Date column when in edit mode. -->
        <DataTemplate x:Key="EditingDateTemplate">
            <DatePicker SelectedDate="{Binding PublishDate}"  />
        </DataTemplate>
    </Grid.Resources>
    <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <!--Custom column that shows the published date-->
            <DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>


The following illustration shows the output from the previous XAML.

A column using a DataTemplate

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Sorting on a Template Column

It isn't clear from the content, but in the DataGridColumn.SortMemberPath Property description:

In columns that are derived from DataGridColumn, such as DataGridTemplateColumn, the SortMemberPath property must be set to enable automatic sorting of the column.

This behavior is different from columns derived from DataGridBoundColumn, such as the DataGridTextColumn and DataGridCheckBoxColumn, which have SortMemberPath set to the property the column is bound to through the Bindings property by default.