Rule for Silverlight float content

I am trying to get a horizontal StackPanel with some text, and then the button got stuck all the way to the right 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> 

This does not work. Obviously, adding a field to a TextBlock will work as follows:

  <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 this is bad for various reasons. Are there any more natural ways to do this?

+7
wpf silverlight
source share
3 answers

Personally, I would use a Grid instead of a StackPanel. Just add two columns, one of which is β€œ*” and one is β€œAuto”, and put your TextBlock in one column, Button in column two:

+8
source share

Use a DockPanel instead:

 <DockPanel> <TextBlock VerticalAlignment="Center" FontSize="14" Margin="5,0,0,0">Ahoy!</TextBlock> <Button DockPanel.Dock="Right" Width="25" Height="25" Style="{StaticResource buttonGlassOrb}" Background="Red" /> </DockPanel> 

By default, the Dock parameter is Left, so it behaves like a horizontal StackPanel for items that do not have an explicit Dock setting.

+3
source share

StackPanels is fine for simplified scenarios, as soon as you want full control over the layout, use the Grid

+2
source share

All Articles