I use this code for row numbers in Datagrid.
<DataGrid Name="DataGrid" LoadingRow="DataGrid_LoadingRow" ...
void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex()).ToString();
}
How to center align these numbers in row header?
<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>
To center align the row numbers in a DataGrid RowHeader, use a custom RowHeaderStyle Template.
You can generate the default template in Visual Studio 2012 or later by right-click on the DataGrid in design mode and select “Edit Additional Templates” -> “Edit RowHeaderStyle” -> “Edit a Copy”. Once you have given the new style resource a name and chosen where to generate it, you can then modify the generated XAML template according to your requirements.
Since each revision of Windows will generate a slightly different template, but on Windows 8 and above, all you have to do is set the
DataGridHeaderBorder
StackPanel in Themes:DataGridHeaderBorder to
HorizontalAlignment="Center"
I'll include a snippet of the Windows 10 Fall Update DataGrid RowHeaderStyle Template.
<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>
Simply style for RowHeader like this
<DataGrid>
<DataGrid.RowHeaderStyle>
<Style TargetType="DataGridRowHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
</DataGrid.RowHeaderStyle>
</DataGrid>
I use it like Cuong Tran Duc answered, but with some padding you can improve the look:
<DataGrid.RowHeaderStyle><Style TargetType="DataGridRowHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="Padding" Value="5 0"></Setter></Style>
</DataGrid.RowHeaderStyle>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.