Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One button on one side and others on other side of StackPanel

I am working on a WPF application. I have a StackPanel in my xaml file. In StackPanel there are three buttons. Problem is that all button are either on left side or on the right side. What I want is that one button is one the left side and two buttons on the right side.

<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" >
    <Button Content="Open"/>
    <Button Content="Close"/>
    <Button Content="Help"/>
</StackPanel>

The output of this is like this. Output

As you can see that there is a lot of space on the left side. I want my Help button to the extreme left side whil Close and Open on the extreme right. I think I can do this by implementing a grid or something like that, but I want to ask that whether I can do this with using stack panel only.

like image 336
fhnaseer Avatar asked Dec 04 '25 07:12

fhnaseer


2 Answers

You cannot do that with a StackPanel, however you don't "have" to use a Grid.

You can use a DockPanel as a compromise

<DockPanel LastChildFill="False">
  <Button Content="Help" DockPanel.Dock="Left" />
  <Button Content="Close" DockPanel.Dock="Right" />
  <Button Content="Open" DockPanel.Dock="Right" />
</DockPanel>

LastChildFill="False" will make sure your last added control does not end up "filling" up all the remaining space thereby giving you the look you want.

like image 97
Viv Avatar answered Dec 06 '25 22:12

Viv


You need to use a Grid for that:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Button Grid.Column="0" Content="Help"/>
    <Button Grid.Column="1" Content="Close"/>
    <Button Grid.Column="2" Content="Open"/>
</Grid>
like image 27
Mike Perrenoud Avatar answered Dec 06 '25 22:12

Mike Perrenoud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!