【问题标题】:How to add a border to the Text inside a TextBlock in WPF?如何在 WPF 中的 TextBlock 内的文本中添加边框?
【发布时间】:2013-07-16 04:53:06
【问题描述】:

我对 WPF 有点陌生,不知道这个问题是不是很奇怪。我想知道是否可以在 textBlock 内的文本周围添加边框。

编辑:

根据建议,我都尝试过,但都没有成功:

<Border BorderBrush="#FF0B232F" BorderThickness="2">
   <TextBlock HorizontalAlignment="Left" Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black"/>      
</Border>

<Label BorderBrush="#FF0B232F" BorderThickness="2,2,2,2" Content="TextBlock" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" />

我在这里做错了吗?

【问题讨论】:

  • 您的代码一切正常。你不喜欢它的什么?
  • 他的问题是如何“在文本块的内部(又名内容)周围添加边框”......没有人回答这个问题。我也想知道怎么做。

标签: wpf textblock


【解决方案1】:

在这种情况下,我使用LabelTextBlock 放置在Border 中。

【讨论】:

    【解决方案2】:

    您的两种方法都是正确的,但是,如果您在网格(或任何其他容器)中声明了文本块/标签,则其内容将会拉伸。 尝试将 VerticalAlignment 和/或 Horizo​​ntalAlignment 设置为适当的设置(上/左、中)..

    类似这样的:

    <Border BorderBrush="#FF0B232F" BorderThickness="2" VerticalAlignment="Top">
        <TextBlock HorizontalAlignment="Left" Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black"/>
    </Border>
    

    【讨论】:

    • Filipe 是正确的 - 如果网格/包含包含其他对象,则边框将有效地覆盖整个网格 - VerticalAlignemnt 和 Horizo​​ntalAlignment 解决了这个问题。不直观!
    【解决方案3】:

    假设您要求在 TextBlock 范围内带有边框覆盖的全尺寸 TextBlock,您可以将其包装在 Grid 中并像这样在 TextBlock 的顶部绘制边框...

    <Grid HorizontalAlignment="Left">
        <TextBlock Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black" />
        <Border BorderBrush="#FF0B232F" BorderThickness="2" />
    </Grid>
    

    因为我们没有在 TextBlock 和 Border 对象上指定网格行和列,所以边框覆盖在 TextBlock 的顶部。

    【讨论】:

      【解决方案4】:

      如果您只想在文本块或任何其他控件周围设置边框,请使用:

       <Border>
            <TextBlock></TextBlock>
          </Border>
      

      您设置边框属性,如颜色、角半径、厚度、...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-07
        • 1970-01-01
        • 2011-06-11
        • 1970-01-01
        相关资源
        最近更新 更多