DataGrid.RowHeaderStyle 属性

定义

获取或设置应用于所有行标题的样式。Gets or sets the style applied to all row headers.

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

属性值

要应用于 DataGrid 中的所有行标题的样式。The style applied to all row headers in the DataGrid. 已注册的默认值为 nullThe registered default is null. 有关可以影响值的因素的更多信息,请参见 DependencyPropertyFor more information about what can influence the value, see DependencyProperty.

示例

下面的示例演示如何通过将具有值转换器的绑定应用于 DataGridRowHeaderContent 属性来在行标题中显示带编号的行。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 : 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

注解

应用 Style 以更新 DataGrid中所有行标题的视觉外观。Apply a Style to update the visual appearance of all the row headers in the DataGrid. 若要为行标题定义 Style,请指定 DataGridRowHeaderTargetTypeTo define a Style for a row header, specify a TargetType of DataGridRowHeader.

你还可以使用 RowHeaderStyle 属性来更新 DataGridRowHeader的任何属性。You can also use the RowHeaderStyle property to update any property of DataGridRowHeader.

Style 可应用于所有行标题或单个行标题。A Style can be applied to all row headers, or to an individual row header. 若要将 Style 应用于单个标题,请设置 DataGridRow.HeaderStyle 属性,该属性优先于 DataGrid.RowHeaderStyle 属性。To apply a Style to an individual header, set the DataGridRow.HeaderStyle property, which takes precedence over the DataGrid.RowHeaderStyle property.

适用于

另请参阅