5

This should be easy: how can I stretch StackPanel inside the DockPanel, so that it would fill the whole parent's content and also maintain it's HorizontalAlignment?

Example:

<DockPanel LastChildFill="True">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Background="Yellow">
        <Button Height="30">Button1</Button>
        <Button Height="30">Button2</Button>
    </StackPanel>
</DockPanel>

In this example the StackPanel's width is the same as the combined width from both buttons (and the background here is yellow), but the remaining space in DockPanel stays white. Looks like the LastChildFille property isn't working in my example.

sventevit
  • 4,766
  • 10
  • 57
  • 89

1 Answers1

3

I think by setting HorizontalAligment to Center on the StackPanel you are overriding the behavior of the DockPanel to fill the whole space with the StackPanel.

When i understand your question right, you want that the full space is yellow and the stackpanel with buttons is centered in the middle. Then you should wrap the StackPanel inside a Grid.

<DockPanel LastChildFill="True">
  <Grid Background="Yellow">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
      <Button Height="30">Button1</Button>
      <Button Height="30">Button2</Button>
    </StackPanel>
  </Grid>
</DockPanel>
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Absolutely right. Another option would be to set `HorizontalAlignment` on the buttons instead, although then they wouldn't necessarily be the same width as each other which is likely to be undesirable. – Matthew Walton Mar 11 '13 at 08:37
  • In this case I could set the background property to DockPanel also. My intent was to create a background style for StackPanel, but this should work also... – sventevit Mar 11 '13 at 09:04