wpf 如何将 CheckBox 与其内容对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7232434/
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
How can I align a CheckBox with its content?
提问by DeveloperDan
The look of a WPF CheckBox misaligns the check portion with the label (content) portion. The check stays slightly above the content as shown here:
WPF CheckBox 的外观使检查部分与标签(内容)部分错位。支票略高于内容,如下所示:
The XAML looks like this:
XAML 看起来像这样:
<CheckBox Content="Poorly aligned CheckBox" Margin="9"/>
The CheckBox is inside a Grid cell. Is there a simple way to make the content and check portions of a XAML CheckBox align vertically?I've tried various combinations of properties with no luck. I saw a similar question herebut the answer is way too complex.
CheckBox 位于 Grid 单元格内。是否有一种简单的方法可以使 XAML CheckBox 的内容和检查部分垂直对齐?我尝试了各种属性组合,但都没有运气。我在这里看到了一个类似的问题,但答案太复杂了。
Thanks in advance.
提前致谢。
EDIT: The problem was caused by the Window FontSize which I set to 14. To re-create the problem set the CheckBox FontSize to 14 (or more). My program is viewed at a distance by factory workers so I allow the Window FontSize to be increased or decreased by the user.
编辑:问题是由我设置为 14 的 Window FontSize 引起的。要重新创建问题,请将 CheckBox FontSize 设置为 14(或更多)。我的程序由工厂工人在远处查看,因此我允许用户增加或减少 Window FontSize。
采纳答案by MichaelS
Edit - New Answer:(previous was no good)
编辑 - 新答案:(以前不好)
Not the best way i believe , but can do the work:
不是我相信的最好方法,但可以完成这项工作:
<CheckBox>
<TextBlock Text="Poorly aligned CheckBox" Margin="0,-2,0,0"/>
</CheckBox>
Using negative margin to push the content up, result:
使用负边距将内容向上推,结果:
回答by Majid
I know it's too late, but here is a better solution, without setting margins. Margins should be set differently for different heights of TextBlock
or Checkbox
.
我知道为时已晚,但这里有一个更好的解决方案,无需设置边距。应为TextBlock
或 的不同高度设置不同的边距Checkbox
。
<CheckBox VerticalAlignment="Center" VerticalContentAlignment="Center">
<TextBlock Text="Well aligned Checkbox" VerticalAlignment="Center" />
</CheckBox>
Update:
更新:
It's worth checking out @nmarler's comment below.
值得查看下面@nmarler 的评论。
回答by Fredrik Hedblad
The default Style of a CheckBox
don't look like that in WPF. It aligns perfectly in both XP and Windows 7. Can you give a better description of how to reproduce this problem?
a 的默认样式与CheckBox
WPF 中的样式不同。它在 XP 和 Windows 7 中完美匹配。您能否更好地描述如何重现此问题?
Two things I can think of to get the offset that you're seeing is either changing the Padding
or the VerticalContentAlignment
. The default CheckBox
value for VerticalContentAlignment
is Top
and a CheckBox
with Content
has Padding
set to "4,0,0,0"
. Try to change these two around and see if it makes any difference.
我能想到的有两件事情得到补偿,你所看到的是要么改变Padding
或VerticalContentAlignment
。的默认CheckBox
值VerticalContentAlignment
是Top
并且 a CheckBox
withContent
已Padding
设置为"4,0,0,0"
。尝试改变这两个,看看它是否有什么不同。
Here is a comparison
这是一个比较
From the following Xaml
从以下 Xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CheckBox Grid.Row="0"
Content="Poorly aligned CheckBox" Margin="9" />
<CheckBox Grid.Row="1"
Content="Padding=4,4,0,0" Margin="9" Padding="4,4,0,0"/>
<CheckBox Grid.Row="2"
Content="Vertical Center" Margin="9"
VerticalContentAlignment="Center"/>
<CheckBox Grid.Row="3"
Content="Vertical Top" Margin="9"
VerticalContentAlignment="Top"/>
</Grid>
回答by LucasM
There is a simple solution for this without needing to make a text block for the content, and without writing extra code that you don't need. Just use "VerticalContentAlignment". Example:
对此有一个简单的解决方案,无需为内容制作文本块,也无需编写不需要的额外代码。只需使用“VerticalContentAlignment”。例子:
<CheckBox Content="Sample of Text" VerticalContentAlignment="Center"/>
回答by Thomas Hetzer
Place an empty CheckBox and the content as separate controls in a StackPanel with horizontal orientation. This works with any font size.
将一个空的 CheckBox 和内容作为单独的控件放置在具有水平方向的 StackPanel 中。这适用于任何字体大小。
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" />
<TextBlock VerticalAlignment="Center" Text="Option X" />
</StackPanel />
回答by landsteven
Applying negative Top padding works just fine for my layout:
应用负顶部填充对我的布局来说很好用:
<CheckBox Content="Poorly aligned CheckBox" Padding="4,-3,0,0" Margin="9"/>
回答by Qwertie
My checkboxes are aligned fine, so I wonder what's different about yours. The only apparent difference is that you are using a larger font than me, which reminds me of the following question:
我的复选框对齐得很好,所以我想知道你的有什么不同。唯一明显的区别是您使用的字体比我大,这让我想起了以下问题:
WPF CheckBox style with the TextWrapping
带有 TextWrapping 的 WPF CheckBox 样式
Your problem may be that if the content is a textblock, the checkbox is top-justified, which may look odd when the font size is increased. So my guess is, try using an AccessText as the Content of the CheckBox instead:
您的问题可能是,如果内容是文本块,则复选框是顶部对齐的,当增加字体大小时,这可能看起来很奇怪。所以我的猜测是,尝试使用 AccessText 作为 CheckBox 的内容:
<CheckBox Margin="9"><AccessText>Better aligned CheckBox?</AccessText></CheckBox>