wpf 将标签与停靠面板的中间对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13142787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Align label to the middle of dock panel
提问by Kev Fixes
I have a dock panel, with one label in the middle and another button on the far right.
我有一个停靠面板,中间有一个标签,最右侧有另一个按钮。
Because of the button the label cannot align to the middle when the windows is maximized.
由于按钮的原因,当窗口最大化时,标签无法与中间对齐。
WPF:
WPF:
<DockPanel Height="40" HorizontalAlignment="Stretch" Margin="-1,-2,0,0" Name="dockPanel1" VerticalAlignment="Stretch" Width="Auto" OpacityMask="{x:Null}">
<Label FontSize="18" Content="Sales" FontWeight="Bold" FontFamily="Arial" Width="883" Height="42" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Foreground="White" DockPanel.Dock="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"></Label>
<Button FontSize="18" Height="47" Width="123" Name="btnStart" Foreground="White" BorderBrush="{x:Null}" FlowDirection="LeftToRight" HorizontalContentAlignment="Center" FontFamily="Arial Rounded MT" ClickMode="Press" DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="0" Content="Start" BorderThickness="0" Focusable="False">
</DockPanel>
回答by Rachel
Use a Gridinstead of a DockPanel
使用 aGrid代替 aDockPanel
Grid's allow objects to be placed on top of each other, so you can position your Label in the middle and the Button on the Right
网格允许对象彼此重叠放置,因此您可以将标签放置在中间,将按钮放置在右侧
<Grid>
<Label HorizontalAlignment="Center" VerticalAlignment="Center" />
<Button HorizontalAlignment="Right" />
</Grid>
Also if you're new to WPF's Layouts, I'd recommend reading through WPF Layouts: A Quick Visual Startso you know what layouts are available and can pick the best one for your sitaution
此外,如果您不熟悉WPF 的布局,我建议您通读WPF 布局:快速视觉入门,以便您了解可用的布局并可以选择最适合您的情况的布局

