Search code examples
wpflayoutgridstackpanel

wpf layout question


<Window ... >
    <StackPanel>
        <Button>b1</Button>
        <Button>b2</Button>
    </StackPanel>
</Window>

how to make this look like this:

<Window ...>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <Button>b1</Button>
        <Button Grid.Row="1">b2</Button>
    </Grid>
</Window>

without using a grid


Solution

  • Short answer: You can't.

    Long answer: Write a custom Panel and override ArrangeOverride and MeasureOverride to simulate Grid behaviour.

    StackPanel arranges each of its child elements to use minimal height (or minimal width if Orientation == Horizontal). StackPanel offers no properties to alter this behaviour. Grid on the other hand, unless indicated otherwise, will divide all available space evenly across each child (or rather row/column).