Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top