21

In my window, there is a Grid that takes up 100% of the available vertical space. Inside that Grid is a StackPanel with another StackPanel and a Frame inside. I would like to have that Frame stick to the bottom of the parent StackPanel.

<StackPanel>
    <StackPanel>
       //normal content
     </StackPanel>
     <Frame /> // should stick to the bottom
</StackPanel>

So that, for example, when the user resizes the Window and the Grid gains height, the Frame inside sticks to the bottom.

I have tried various things, using VerticalAlign="Stretch" or a DockPanel and assigning the Frame a DockPanel.Dock="Bottom", but to no success. I'd be thankful for a hint or two. I don't need the StackPanel to be a StackPanel, it can also be a DockPanel.

peter
  • 2,103
  • 7
  • 25
  • 51

3 Answers3

39

A StackPanel is not the correct Panel to use for your requirements as they do not provide the same layout and resizing capabilities as a Grid. Therefore, simply replace it with a Grid, give the majority of the space to the inner StackPanel and then the Frame will stick to the bottom.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />   
    </Grid.RowDefinitions>
    <StackPanel>
       //normal content
     </StackPanel>
     <Frame Grid.Row="1" />
</Grid>

You can find out more about the different WPF Panels in the Panels Overview page on MSDN.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
15

Looks more like a use case for DockPanel

<DockPanel>
    <StackPanel>
        <!--//normal content-->
    </StackPanel>
    <Frame DockPanel.Dock="Bottom"/> <!--// should stick to the bottom-->
</DockPanel>
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
  • 1
    A `DockPanel` is *not* the appropriate `Panel` to use in this situation as they are heavyweight controls and the functionality that they provide is not required here. A simple `Grid` would be far more efficient. – Sheridan Apr 17 '15 at 12:38
  • 6
    Difficult to say if DockPanel is not appropriate, without knowing the use case. I will let @Peter decide. – Manish Basantani Apr 17 '15 at 12:43
  • 1
    @Sheridan A DockPanel actually provides less functionality than a Grid and has better performance: https://stackoverflow.com/a/9996707/1988326 – mark.monteiro Aug 26 '19 at 22:07
  • That list is NOT an ordered list and does not take into consideration a layout as simple as the one that we are discussing here. The time-consuming parts of a `Grid` control come from complex `ColumnDefinition` and `RowDefinition` setups, along with its star sizing capabilities. As always, it depends on how it will be used. – Sheridan Sep 03 '19 at 11:19
-1

According to MSDN docs about StackPanel

A StackPanel Arranges child elements into a single line that can be oriented horizontally or vertically.

So in your case, StackPanel is not the right control to use. If you want your control to be fixed at a particular position, you can use DockPanel and place your control inside it.

<DockPanel>
        <Frame DockPanel.Dock="Bottom"/>
</DockPanel>
sohaiby
  • 1,168
  • 3
  • 24
  • 39