Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stick to the bottom of a StackPanel

Tags:

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.

like image 905
peter Avatar asked Apr 17 '15 12:04

peter


People also ask

What is the difference between StackPanel and DockPanel?

For example, the order of child elements can affect their size in a DockPanel but not in a StackPanel. This is because StackPanel measures in the direction of stacking at PositiveInfinity, whereas DockPanel measures only the available size. The following example demonstrates this key difference.

How do you make a StackPanel scroll?

Put ScrollViewer inside StackPanel . You could use a ListBox instead. It will scroll automatically.

What is StackPanel?

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.


2 Answers

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.

like image 95
Sheridan Avatar answered Sep 27 '22 22:09

Sheridan


Looks more like a use case for DockPanel

<DockPanel>
    <StackPanel>
        <!--//normal content-->
    </StackPanel>
    <Frame DockPanel.Dock="Bottom"/> <!--// should stick to the bottom-->
</DockPanel>
like image 31
Manish Basantani Avatar answered Sep 27 '22 20:09

Manish Basantani