DataGrid.RowHeaderStyle Property

Definition

Gets or sets the style applied to all row headers.

C#
public System.Windows.Style RowHeaderStyle { get; set; }

Property Value

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.

Examples

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.

XAML
<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>
C#
public class ConvertItemToIndex : IValueConverter
{
    #region IValueConverter Members
    //Convert the Item to an Index
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            //Get the CollectionView from the DataGrid that is using the converter
            DataGrid dg = (DataGrid)Application.Current.MainWindow.FindName("DG1");
            CollectionView cv = (CollectionView)dg.Items;
            //Get the index of the item from the CollectionView
            int rowindex = cv.IndexOf(value)+1;

            return rowindex.ToString();
        }
        catch (Exception e)
        {
            throw new NotImplementedException(e.Message);
        }
    }
     //One way binding, so ConvertBack is not implemented
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Remarks

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.

Applies to

Product Versions
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also