I'm trying to get a horizontal StackPanel with some text, and then a button stuck all the way to the right hand side. I tried this:
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" FontSize="14" Margin="5,0,0,0">Ahoy!</TextBlock>
<Button HorizontalAlignment="Right" Width="25" Height="25" Style="{StaticResource buttonGlassOrb}" Background="Red" />
</StackPanel>
Which doesn't seem to work. Obviously adding a margin to the TextBlock will work, like this:
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" FontSize="14" Margin="5,0,120,0">Ahoy!</TextBlock>
<Button HorizontalAlignment="Right" Width="25" Height="25" Style="{StaticResource buttonGlassOrb}" Background="Red" />
</StackPanel>
But that's bad for all sorts of reasons. Are there any more natural ways to do this?
Personally, I would use a Grid instead of a StackPanel for this. Just add two columns, one set to size "*" and one to "Auto", and put your TextBlock in column one, Button in Column two:
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