共用方式為


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

備註

套用 以 Style 更新 中 DataGrid 所有資料列標頭的視覺外觀。 若要定義 Style 資料列標頭的 ,請指定 TargetTypeDataGridRowHeader

您也可以使用 RowHeaderStyle 屬性來更新 的任何屬性 DataGridRowHeader

Style可以套用至所有資料列標頭,或套用至個別的資料列標頭。 若要將 套用 Style 至個別標頭,請設定 DataGridRow.HeaderStyle 優先于 DataGrid.RowHeaderStyle 屬性的屬性。

適用於

另請參閱