如何居中对齐WPF中Datagrid行标题中的行号

时间:2017-12-28 05:32:01

标签: c# wpf datagrid

我将此代码用于Datagrid中的行号。

<DataGrid Name="DataGrid" LoadingRow="DataGrid_LoadingRow" ...

void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
   e.Row.Header = (e.Row.GetIndex()).ToString(); 
}

如何在行标题中居中对齐这些数字?

4 个答案:

答案 0 :(得分:4)

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" LoadingRow="OnLoadingRow">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
    </DataGrid.Columns>
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <Label HorizontalAlignment="Center"
                   Content="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow},
                                     Path=Header}"/>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>

答案 1 :(得分:1)

要在DataGrid RowHeader中居中对齐行号,请使用自定义RowHeaderStyle模板。

您可以在Visual Studio 2012或更高版本中生成默认模板,方法是在设计模式下右键单击DataGrid,然后选择“编辑其他模板” - &gt; “编辑RowHeaderStyle” - &gt; “编辑副本”。一旦为新样式资源指定了名称并选择了生成它的位置,就可以根据需要修改生成的XAML模板。

由于Windows的每个版本都会生成略有不同的模板,但在Windows 8及更高版本上,您只需将主题:DataGridHeaderBorder中的 DataGridHeaderBorder StackPanel设置为

  

的Horizo​​ntalAlignment = “中心”

我将包含Windows 10 Fall Update DataGrid RowHeaderStyle模板的片段。

<DataGrid.Resources>
            <BooleanToVisibilityConverter x:Key="bool2VisibilityConverter"/>
            <Style x:Key="RowHeaderGripperStyle" TargetType="{x:Type Thumb}">
                <Setter Property="Height" Value="8"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Cursor" Value="SizeNS"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Thumb}">
                            <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="DataGridRowHeaderStyle1" TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                            <Grid>
                                <Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" IsSelected="{TemplateBinding IsRowSelected}" Orientation="Horizontal" Padding="{TemplateBinding Padding}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                        <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                                        <Control SnapsToDevicePixels="false" Template="{Binding ValidationErrorTemplate, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Visibility="{Binding (Validation.HasError), Converter={StaticResource bool2VisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/>
                                    </StackPanel>
                                </Themes:DataGridHeaderBorder>
                                <Thumb x:Name="PART_TopHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Top"/>
                                <Thumb x:Name="PART_BottomHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Bottom"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>

答案 2 :(得分:1)

简单地为RowHeader设置样式

        <DataGrid>
            <DataGrid.RowHeaderStyle>
                <Style TargetType="DataGridRowHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
                </Style>
            </DataGrid.RowHeaderStyle>
        </DataGrid>

答案 3 :(得分:0)

我像Cuong Tran Duc回答的那样使用它,但是使用一些填充可以改善外观:

<DataGrid.RowHeaderStyle><Style TargetType="DataGridRowHeader">
    <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
    <Setter Property="Padding" Value="5 0"></Setter></Style>
</DataGrid.RowHeaderStyle>