简体   繁体   中英

WPF Datagrid HeaderColumn Content Alignment

The ContentTemplate code I use for the Datagrid Header is included in the relevant style file as follows:

<Style TargetType="{x:Type DataGrid}">
...
<Style.Resources>
    <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding}" Margin="10 5" Grid.Column="0" />
                                <Button x:Name="btnFilter" Content="&#xf0d7;" FontFamily="{StaticResource FontAwesome}" FontSize="16" HorizontalAlignment="Right" Grid.Column="1" />
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
    </Style>
</Style.Resources>
...
</Style>

The resulting image from this code is as follows:

在此处输入图像描述

But I want to align the arrow icons to the right side as in the picture below. How can I do that?

在此处输入图像描述

Set the HorizontalContentAlignment property of the DataGridColumnHeaders to Stretch :

<Style TargetType="DataGridColumnHeader">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="ContentTemplate">
        ...
    </Setter>
</Style>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM