我试图获得带有一些文本的水平StackPanel,然后一个按钮一直粘到右侧.我试过这个:
<StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" FontSize="14" Margin="5,0">Ahoy!</TextBlock> <Button HorizontalAlignment="Right" Width="25" Height="25" Style="{StaticResource buttonGlassOrb}" Background="Red" /> </StackPanel>
这似乎不起作用.显然,为TextBlock添加一个边距将起作用,如下所示:
<StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" FontSize="14" Margin="5,120,0">Ahoy!</TextBlock> <Button HorizontalAlignment="Right" Width="25" Height="25" Style="{StaticResource buttonGlassOrb}" Background="Red" /> </StackPanel>
但是由于种种原因这很糟糕.还有更自然的方法吗?
解决方法
就个人而言,我会使用Grid而不是StackPanel.只需添加两列,一组设置为“*”,一组设置为“Auto”,然后将TextBlock放入第一列,按钮放入第二列: