How to apply a style to the LAST ROW of the DataGrid

Feb 15, 2010 at 2:58 PM


I want to apply a style for the last row of the datagrid. Is there any easy way of doing it? 


Feb 16, 2010 at 1:32 PM

Are you talking about the row that is added by the DataGrid itself?  if so you can create a ControlTemplate in your XAML.  I generally place it in the Resources for the DataGrid.

<ControlTemplate x:Key="LastRow_ControlTemplate" TargetType="{x:Type dg:DataGridRow}">
    <Border x:Name="DGR_Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
            <Rectangle Grid.Column="1"/>

Then in the LoadingRow Event for the DataGrid we look for the NewItemPlaceholder and set its ControlTemplate to the one we've created in XAML
Private Sub myDataGrid_LoadingRow(ByVal sender As Object, ByVal e As Microsoft.Windows.Controls.DataGridRowEventArgs) Handles myDataGrid.LoadingRow
'We set the ControlTemplate and Height on the very last row in the running order, which is the NewItemPlaceHolder.  This is not part of our data.  It is added by the WPF DataGrid.
    If e.Row.Item IsNot GetType(DBNull) Then
        If e.Row.Item Is CollectionView.NewItemPlaceholder Then
            e.Row.Template = TryCast(FindResource("LastRow_ControlTemplate"), ControlTemplate)
            e.Row.Height = 20
        End if
    End if
End Sub

Vinsibal has a really good write up on his blog about it if you need further details.