DataGrid.RowHeaderStyle Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the style applied to all row headers.

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

Public Property RowHeaderStyle As Style

Property Value

Type: System.Windows.Style

The style applied to all row headers in the DataGrid. The registered default is null. For more information about what can influence the value, see DependencyProperty.

Apply a Style to update the visual appearance of all the row headers in the DataGrid. To define a Style for a row header, specify a TargetType of DataGridRowHeader.

You can also use the RowHeaderStyle property to update any property of DataGridRowHeader.

A Style can be applied to all row headers, or to an individual row header. To apply a Style to an individual header, set the DataGridRow.HeaderStyle property, which takes precedence over the DataGrid.RowHeaderStyle property.

The following example shows how to display numbered rows in the row header by applying a binding with a value converter to the Content property of the DataGridRowHeader. The converter is created as a resource by mapping the namespace and creating an instance of the class. For more information, see Data Binding Overview.

<Window.Resources>
    <local:ConvertItemToIndex x:Key="IndexConverter"/>     
</Window.Resources>
<Grid>
    <DataGrid Name="DG1" ItemsSource="{Binding}" CanUserAddRows="False" CanUserDeleteRows="False" >
        <!--Bind the Content property of the RowHeaderStyle to the Converter to create numbered rows-->
        <DataGrid.RowHeaderStyle>
            <Style TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Content" Value="{Binding Converter={StaticResource IndexConverter}}" />
            </Style>
        </DataGrid.RowHeaderStyle>
    </DataGrid>
</Grid>
Public Class ConvertItemToIndex
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Try
            'Get the DataRowView that is being passed to the Converter 
            Dim drv As System.Data.DataRowView = DirectCast(value, System.Data.DataRowView)
            'Get the CollectionView from the DataGrid that is using the converter 
            Dim dg As DataGrid = DirectCast(Application.Current.MainWindow.FindName("DG1"), DataGrid)
            Dim cv As CollectionView = DirectCast(dg.Items, CollectionView)
            'Get the index of the DataRowView from the CollectionView 
            Dim rowindex As Integer = cv.IndexOf(drv) + 1

            Return rowindex.ToString()

        Catch e As Exception
            Throw New NotImplementedException(e.Message)
        End Try
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

.NET Framework
Available since 4.0
Silverlight
Available since 2.0
Return to top
Show: