0

I have this WPF button in my StackPanel:

<StackPanel Grid.ColumnSpan="6" Grid.Row="12" Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="LightGray" Height="40">
            <Button x:Name="btnSave" Click="btnSave_Click" Content="{x:Static res:Strings.ToolPanelEditView_Button_Save}" HorizontalAlignment="Right" />
</StackPanel>

For some reason, the button shows on the left although I set its HorizontalAlignment to Right:

enter image description here

How can I make my save button show on the right?

P.S. when I change the StackPanel's HorizontalAlignment to Right instead of Stretch, the button does show on the right like it should (but then the gray background of the StackPanel does not stretch either...

user1028741
  • 2,745
  • 6
  • 34
  • 68
  • Try using a `Grid` instead of a `StackPanel` – chathux Aug 30 '15 at 10:45
  • I didn't show it on the snippset, but I have an additional button there (cancel button). If I change the `StackPanel` to `Vertical` it will make those buttons be one above the other... – user1028741 Aug 30 '15 at 10:51

2 Answers2

0

I think that a Grid is more appropriate for your case

 <Grid  HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="LightGray" Height="40">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="btnSave" Grid.Column="1" Content="Save"  HorizontalAlignment="Right"  />
        <!--Your Other Button Grid.Column="2"-->
    </Grid>

or you could as well replace the stackPanel with a DockPanel

Community
  • 1
  • 1
SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
0

You can use DockPanel as container, stretch it and dock your button to the right side

    <DockPanel Grid.ColumnSpan="6" Grid.Row="12" HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="LightGray" Height="40">
      <Button Content="Save" DockPanel.Dock="Right"/>
      <Button Content="Cancel" DockPanel.Dock="Right"/>
    </DockPanel>
Ali Baqbani
  • 153
  • 1
  • 2
  • 13