【问题标题】:How to vertically center CheckBox graphic with respect to wrapping content如何相对于包装内容垂直居中 CheckBox 图形
【发布时间】:2018-02-28 08:19:32
【问题描述】:

我有以下 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 的实际图形是在做自己的事情。这是因为文本换行 - 如果它是单行文本,它将全部居中。

如何使图形与文本(即 CheckBox 的内容)垂直居中?

【问题讨论】:

    标签: uwp centering uwp-xaml word-wrap


    【解决方案1】:

    几分钟后我从this question找到了答案。

    1. 在 Visual Studio 的 Document Outline 中选择 CheckBox
    2. 右键单击复选框 > 编辑模板 > 编辑副本
    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. 将 Grid 的 VerticalAlignment 属性从 Top 更改为 Center

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-05
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多