DataGrid.RowHeaderStyle 属性

定义

获取或设置应用于所有行标题的样式。

public:
 property System::Windows::Style ^ RowHeaderStyle { System::Windows::Style ^ get(); void set(System::Windows::Style ^ value); };
public System.Windows.Style RowHeaderStyle { get; set; }
member this.RowHeaderStyle : System.Windows.Style with get, set
Public Property RowHeaderStyle As Style

属性值

Style

要应用于 DataGrid 中的所有行标题的样式。 已注册的默认值为 null。 有关可以影响值的因素的更多信息,请参见 DependencyProperty

示例

下面的示例演示如何通过将具有值转换器的绑定应用到 ContentDataGridRowHeader的属性来显示行标题中的编号行。 转换器通过映射命名空间和创建类的实例来创建为资源。 有关详细信息,请参阅 数据绑定概述

<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 : 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
}
Public Class ConvertItemToIndex
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Try
            '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 item from the CollectionView 
            Dim rowindex As Integer = cv.IndexOf(value) + 1

            Return rowindex.ToString()

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

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

注解

应用 a Style 以更新所有行标题的 DataGrid视觉外观。 若要定义Style行标题,请指定一DataGridRowHeaderTargetType

还可以使用该 RowHeaderStyle 属性更新任何属性 DataGridRowHeader

可以将 A Style 应用于所有行标题或单个行标题。 若要向单个标头应用属性 Style ,请设置 DataGridRow.HeaderStyle 优先于该属性的属性 DataGrid.RowHeaderStyle

适用于

另请参阅