UWP,XAML-使CheckBox为空 [英] UWP, XAML - making CheckBox empty
问题描述
如何使CheckBox为空?我只需要打勾.现在,它需要更多的空白空间,例如:
How can I make CheckBox empty? I only need the tick. Now it takes additional empty space, like here:
<CheckBox
Background="Aqua"
Margin="0,0,0,0"/>
(我添加了颜色以查看该控件占用了多少空间(空白空间会引起问题).
(I added color to look how much space this control takes (that empty space causes alligning problems)).
我只需要刻度矩形,我不希望有空白空间.我该如何实现?
I just need the tick rectangle, I don't want the empty space. How can I achieve that?
将边距"设置为零,将内容"设置为"无效.
Setting Margin to zeros and Content to "" doesn't work.
推荐答案
-
如果只想删除右侧的空白区域,则可以通过将Padding和MinWidth添加到0来实现:
If you just want to remove the empty space on the right, you can do it by Adding Padding and MinWidth to 0:
<CheckBox Background="Aqua" Padding="0" MinWidth="0"/>
它看起来如下:
-
如果您还想删除顶部和底部的空间,则需要做更多的工作:
If you also want to remove the space at the top and bottom, you need more works:
- 2a.在Visual Studio中选择文档大纲中的复选框
- 2b.右键单击复选框->编辑模板->编辑副本
- 2c.然后您可以看到直接编辑CheckBox样式
-
2d.然后在ControlTemplate中找到以下代码
- 2a. Select the CheckBox in Document Outline in Visual Studio
- 2b. Right Click the CheckBox -> Edit Template -> Edit a Copy
- 2c. Then you can see edit the CheckBox style directly
2d. Then locate the following code in ControlTemplate
<Grid Height="32" VerticalAlignment="Top">
<Rectangle x:Name="NormalRectangle" Fill="Transparent" Height="20" Stroke="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" StrokeThickness="{ThemeResource CheckBoxBorderThemeThickness}" UseLayoutRounding="False" Width="20"/>
<FontIcon x:Name="CheckGlyph" Foreground="{ThemeResource SystemControlHighlightAltChromeWhiteBrush}" FontSize="20" FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="" Opacity="0"/>
</Grid>
如您所见,Rectangle为20x20,但它具有一个高度为32的容器网格.只需将Grid的高度从32修改为20,将如下所示:
As you can see the Rectangle is 20x20 but it have a container grid with 32 height. Just edit the Grid's height from 32 to 20 will look as below:
哦,等等,为什么它的高度仍然是32?这是因为它在样式中设置了MinWidth和MinHeight
Oh wait, why is it still have a height of 32? It is because it have MinWidth and MinHeight set in the style
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="32"/>
最后,您可以在此处直接将这两个值都重置为0,也可以像在步骤1中那样将MinHeight设置为外部.
Finally, you can directly reset both value to 0 here, or just set the MinHeight outside as in Step 1.
这篇关于UWP,XAML-使CheckBox为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!