Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid expander header - can't get arrangement right

Tags:

c#

wpf

I have the following Expander defined for a DataGrid.

<Expander IsExpanded="True" HorizontalAlignment="Stretch" Background="Blue">
    <Expander.Header>
            <Grid HorizontalAlignment="Stretch" Background="BurlyWood">
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                      </Grid.ColumnDefinitions>

                      <TextBlock Text="{Binding Path=Name, StringFormat=\{0:D\}}" FontWeight="Bold" />
                      <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1">
                        <TextBlock Text="Total : "/>
                        <TextBlock Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" FontWeight="Bold"/>
                      </StackPanel>
                </Grid>
        </Expander.Header>
        <ItemsPresenter />
</Expander>

I need to display the item name in the left and the sum in the right end of the group header. However what I get is this:

enter image description here

How can I move the 'Total' to the right end of the header?

like image 672
nakiya Avatar asked Jul 20 '12 08:07

nakiya


1 Answers

I had the same problem and if I remember it right the problem is that the ContentPresenter for Expander.Header doesn't care the HorizontalAlignment of Expander. I found somewhere this nice workaround:

<Expander.Header>
    <Grid HorizontalAlignment="{Binding Path=HorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">
        ...
    </Grid>
</Expander.Header>
like image 104
LPL Avatar answered Oct 12 '22 11:10

LPL