xaml - 如何垂直对齐 XAML UWP 复选框
问题描述
我正在尝试垂直对齐复选框。实际的框出现在顶部。我希望它出现在中间。
这是 XAML:
<CheckBox FontSize="100" Content="Test" VerticalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
这是它的样子:
我在 SO 上看到过类似的问题,但答案是使用 VerticalContentAlignment。这不起作用。一些答案建议使用不缩放的负填充或边距。所以请不要标记为重复,这还没有真正得到回答。或者也许它是为 WPF 回答的,但不是为 UWP 回答的。
我知道您可以更改控件的样式,但是对于应该开箱即用的东西来说,这是很多工作。
我正在使用 VS 2019 16.6.2 和 UWP v6.2.10
更新
这就是我希望我的 CheckBox 看起来的样子。
解决方案
如何垂直对齐 XAML UWP 复选框
对于需求,更好的方法是修改复选框的默认布局。默认布局是Column
,我们需要将其更改为Row
. 我将分享样式的一部分,您可以直接使用它。
<Grid
x:Name="RootGrid"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
>
<Grid.RowDefinitions>
<RowDefinition Height="32" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid
Height="32"
HorizontalAlignment="Center"
VerticalAlignment="Top"
>
<Rectangle
x:Name="NormalRectangle"
Width="20"
Height="20"
Fill="{ThemeResource CheckBoxCheckBackgroundFillUnchecked}"
Stroke="{ThemeResource CheckBoxCheckBackgroundStrokeUnchecked}"
StrokeThickness="{ThemeResource CheckBoxBorderThemeThickness}"
UseLayoutRounding="False"
/>
<FontIcon
x:Name="CheckGlyph"
FontFamily="{ThemeResource SymbolThemeFontFamily}"
FontSize="20"
Foreground="{ThemeResource CheckBoxCheckGlyphForegroundUnchecked}"
Glyph=""
Opacity="0"
/>
</Grid>
<ContentPresenter
x:Name="ContentPresenter"
Grid.Row="1"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="Center"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
AutomationProperties.AccessibilityView="Raw"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}"
TextWrapping="Wrap"
/>
完整样式请参考此链接。
更新
如果不想编辑默认样式,我们可以自定义复选框,并在 OnApplyTemplate
方法中编辑布局。
public class CustomCheckBox : CheckBox
{
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
var normalRectangle = GetTemplateChild("NormalRectangle") as Rectangle;
var rootGrid = normalRectangle.Parent as Grid;
Grid.SetColumnSpan(rootGrid, 2);
rootGrid.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
var contentPresenter = GetTemplateChild("ContentPresenter") as ContentPresenter;
Grid.SetColumn(contentPresenter, 0);
Grid.SetColumnSpan(contentPresenter, 2);
contentPresenter.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
}
}
用法
<local:CustomCheckBox FontSize="100" Content="Test" VerticalAlignment="Center" HorizontalAlignment="Center" />
更新 1
使用以下内容替换OnApplyTemplate
内容。
var normalRectangle = GetTemplateChild("NormalRectangle") as Rectangle;
var rootGrid = normalRectangle.Parent as Grid;
Grid.SetColumnSpan(rootGrid, 2);
rootGrid.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left
rootGrid.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
推荐阅读
- angular - 如何通过 Typescript 发布 GMT 日期?
- javascript - 函数未返回,但 console.log 显示内容
- sql - 存储过程 SQL 中的比较运算符
- flutter - Flutter beta SDK >= 1.22.0 - IDE 问题
- mysql - 我可以在第一个插入中连接最后一个插入 id 吗?
- r - 根据条件组合行
- javascript - 如何摆脱JS中的第一个特定字符串
- svg - Animejs SVG 路径变形 - 如何使用贝塞尔曲线绘制圆
- python - 如何尽可能高效地进行一维离散碰撞检测?
- android - 最新材料设计版本中的渲染问题“不支持 Path.op()”