Search code examples
c#wpfxamlstackpanel

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.


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.