Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal Stackpanel Fill Parent Control

Is it possible to have a <StackPanel> or labels (or buttons, whatever) going horizontally across it's parent control (e.g. Window) and have it fill out all the space allowed?

For example if I had 3 controls

_ window width__

[ 1 ]__ [ 2 ] __ [ 3 ]

or 2 controls

_ window width__

[ 1 ]_______ [ 2 ]

in each case the full width of the window is being utilized with each edge control being left and right justified respectively.

like image 572
Ternary Avatar asked Mar 28 '12 16:03

Ternary


People also ask

What is the difference between StackPanel and DockPanel?

StackPanel vs. 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.

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.

What is StackPanel C#?

Example. A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.


1 Answers

A StackPanel will stack controls, so no is the short answer. That's not what a StackPanel is designed to do.

You could use a grid as Vlad suggested. Something like this:

    <Grid HorizontalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <Button Width="20" Height="20" Grid.Column="0"/>
        <Button Width="20" Height="20" Grid.Column="2"/>
        <Button Width="20" Height="20" Grid.Column="4"/>
    </Grid>
like image 125
Matt Burland Avatar answered Oct 18 '22 21:10

Matt Burland