1

The right-aligned label doesn't reach the right border:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
    <Label Content="Left Align" HorizontalAlignment="Left"/>
    <Label Content="Right Align" HorizontalAlignment="Right" />
</StackPanel>

It seems that both labels don't take up full available width even though I've specified HorizontalAlignment="Stretch" . What is the cause of it?

enter image description here

Bogdan Verbenets
  • 25,686
  • 13
  • 66
  • 119

3 Answers3

0

You should use a DockPanel instead of a StackPanel for that.

See also Align items in a stack panel? or

<DockPanel LastChildFill="False">
    <Label Content="Left Align" DockPanel.Dock="Left"/>
    <Label Content="Right Align" DockPanel.Dock="Right" />
</DockPanel>

And @santosc described here why the alignment with StackPanel doesn't work as you expected:

A stackpanel, no matter how you stretch it, will collapse around the children. you can't make it grow more than that.

Community
  • 1
  • 1
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
0

The StackPanel shrinks to fit the content if no width is provided.

Use a DockPanel,as Stephan Bauer said, or possibly a Grid.

Miklar
  • 31
  • 1
  • 3
0

Try it :

     <Grid>
       <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
       </Grid.ColumnDefinitions>
       <Label Grid.Column="0" Content="Left Align" />
       <Label Grid.Column="2" Content="Right Align"  />
    </Grid>
Cassius
  • 159
  • 1
  • 8