简体   繁体   中英

WPF right align button in DataGrid Column Header

I'm looking at adding a filter button to the right hand side of my datagrid column header. I have succeeded in adding the button and have implemented all of the functionality I need I just can't seem to get the button to be right aligned in the header.

Here is my current XAML:

<DataGridTextColumn Width="2*" IsReadOnly="True" Binding="{Binding Load}" x:Name="temp">
    <DataGridTextColumn.Header>
        <Grid Margin="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="16"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="Load" />
            <Button Grid.Column="1" x:Name="btnFilter" Content="+" Margin="3,0,0,0" Click="btnFilter_Click"></Button>
        </Grid>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

This is how it looks currently and where I want the button to be positioned.

例

I thought using the grid would do the trick but when I select the grid and look at the designer it's width isn't the entire width of the column only the width for the textblock and button.

What am I missing in order to have the button right aligned in the header?

Use the HeaderStyle of the column to set the HorizontalContentAlignment property of the DataGridColumnHeader to Stretch:

<DataGridTextColumn ...>
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </DataGridTextColumn.HeaderStyle>
    <DataGridTextColumn.Header>
        <Grid Margin="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="16"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="Load" />
            <Button Grid.Column="1" x:Name="btnFilter" Content="+" Margin="3,0,0,0" HorizontalAlignment="Right"></Button>
        </Grid>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

While mm8's answer is perfectly correct, it would be (I believe) slightly more efficient to use a DockPanel, and it's certainly more consise:

<DataGridTextColumn ...>
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </DataGridTextColumn.HeaderStyle>
    <DataGridTextColumn.Header>
        <DockPanel>
            <Button DockPanel.Dock="Right" x:Name="btnFilter" Content="+" Margin="3,0,0,0"></Button>
            <TextBlock Text="Load" />
        </DockPanel>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

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