如何垂直居中CheckBox图形与包装内容

时间:2018-02-27 23:59:22

标签: uwp centering uwp-xaml textwrapping

我有以下XAML结构

<StackPanel Orientation="Horizontal">

    <CheckBox>
        <TextBlock Text="Lorem ipsum dolor sit amet etc" TextWrapping="Wrap" Width="200"/>
    </CheckBox>

    <Button>
        <SymbolIcon Symbol="Delete"/>
    </Button>

</StackPanel>

注意删除按钮和文本(严格来说整个CheckBox)是如何垂直居中的,但CheckBox的实际图形是自己做的事情。这是因为文本换行 - 如果它是单行文本,它将全部居中。

enter image description here

如何让图形与文本垂直居中(这是CheckBox的内容)?

1 个答案:

答案 0 :(得分:1)

我在this question之后几分钟找到答案。

  1. 在Visual Studio中选择文档大纲中的CheckBox
  2. 右键单击CheckBox&gt;编辑模板&gt;编辑副本
  3. 在ControlTemplate中找到以下代码

    <Grid Height="32" VerticalAlignment="Top">
        <Rectangle x:Name="NormalRectangle" Fill="{ThemeResource CheckBoxCheckBackgroundFillUnchecked}" Height="20" StrokeThickness="{ThemeResource CheckBoxBorderThemeThickness}" Stroke="{ThemeResource CheckBoxCheckBackgroundStrokeUnchecked}" UseLayoutRounding="False" Width="20"/>
        <FontIcon x:Name="CheckGlyph" FontFamily="{ThemeResource SymbolThemeFontFamily}" Foreground="{ThemeResource CheckBoxCheckGlyphForegroundUnchecked}" FontSize="20" Glyph="&#xE001;" Opacity="0"/>
    </Grid>
    
  4. 将网格的VerticalAlignment属性从Top更改为Center