为什么我的 WPF 单选按钮没有垂直居中对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25008776/
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
Why aren't my WPF radio buttons vertically aligned to the center?
提问by ssb
I have a WPF project where I'm trying to use radio buttons to determine which TextBoxinput to use. When I run it, though, the radio button itself is aligned to the top of the container, and I cannot find any sort of alignment property that affects it. Is this behavior expected? I've been searching for answers but everything seems to be asking how to align the radio button vertically. My assumption is that it's related to how I have it nested in other controls, but how can I make it work without changing too much?
我有一个 WPF 项目,我正在尝试使用单选按钮来确定TextBox要使用的输入。但是,当我运行它时,单选按钮本身与容器的顶部对齐,我找不到任何影响它的对齐属性。这种行为是预期的吗?我一直在寻找答案,但似乎一切都在询问如何垂直对齐单选按钮。我的假设是它与我如何将它嵌套在其他控件中有关,但是如何在不进行太多更改的情况下使其工作?
This is the xaml relevant to the radio buttons:
这是与单选按钮相关的 xaml:
<DockPanel Grid.Column="1" Margin="5,0,0,0">
<RadioButton HorizontalContentAlignment="Stretch" DockPanel.Dock="Top" IsChecked="True">
<xctk:TimePicker Name="TimePickerBox" Margin="0" Format="LongTime"
VerticalAlignment="Center"/>
</RadioButton>
<RadioButton Margin="0,5,0,0" DockPanel.Dock="Top">
<StackPanel Orientation="Horizontal">
<TextBox Name="Hours" Width="30" VerticalAlignment="Center">0</TextBox>
<Label>Hours</Label>
<TextBox Name="Minutes" Width="30" VerticalAlignment="Center">0</TextBox>
<Label>Minutes</Label>
<TextBox Name="Seconds" Width="30" VerticalAlignment="Center">0</TextBox>
<Label>Seconds</Label>
</StackPanel>
</RadioButton>
// ...
This is what it looks like:
这是它的样子:


How can I get the radio buttons to be centered vertically?
如何让单选按钮垂直居中?
回答by Rohit Vats
Set VerticalContentAlignmentas Centerfor RadioButton:
设置VerticalContentAlignment为Center对单选按钮:
<RadioButton Margin="0,5,0,0" DockPanel.Dock="Top"
VerticalContentAlignment="Center">
.....
</RadioButton>
回答by Jason Z
On both RadioButtonelements, try adding VerticalAlignment="Top" VerticalContentAlignment="Center". I did that in a test project and it seemed to do the trick.
在这两个RadioButton元素上,尝试添加VerticalAlignment="Top" VerticalContentAlignment="Center". 我在一个测试项目中做到了这一点,它似乎奏效了。
回答by Nicolas
I copied your XAML and it does exactly what you want it to do. So it seems there might be another reason to this.
Note that a radioButtons Bullet will always align to the first line of a textblock inside of it.
For further reading see here:
How do I make a RadioButton's Bullet align top?for an explanation.
我复制了您的 XAML,它完全符合您的要求。所以这似乎还有另一个原因。
请注意,radioButtons Bullet 将始终与其中的文本块的第一行对齐。
如需进一步阅读,请参见此处:
如何使 RadioButton 的 Bullet 对齐顶部?为解释。
回答by Cheese
I don't really remember what DockPanel.Dockdoes, but try setting StackPanel's VerticalAligmentto Center, and if it doesn't help also set the DockPanel.Dockto Center
我真的不记得有什么DockPanel.Dock作用,但尝试设置StackPanel's VerticalAligmentto Center,如果它没有帮助,也设置DockPanel.DocktoCenter
<RadioButton Margin="0,5,0,0" DockPanel.Dock="Center">
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<TextBox Name="Hours" Width="30" VerticalAlignment="Center">0</TextBox>
<Label>Hours</Label>
<TextBox Name="Minutes" Width="30" VerticalAlignment="Center">0</TextBox>
<Label>Minutes</Label>
<TextBox Name="Seconds" Width="30" VerticalAlignment="Center">0</TextBox>
<Label>Seconds</Label>
</StackPanel>
</RadioButton>

