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.

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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With