【问题标题】:How to define stroke around background rectangle of textblock in WPF?如何在 WPF 中定义文本块背景矩形周围的笔划?
【发布时间】:2010-12-23 16:56:13
【问题描述】:

我想在一个矩形内放置一些文本,该矩形周围有笔划(就像对象“Rectangle”本身的属性“Stroke”一样)。但我没能找到定义这种笔画的对象“Textblock”的属性。

【问题讨论】:

    标签: c# wpf xaml text layout


    【解决方案1】:

    您可以将TextBlock 放在Border 中,并设置Border 的属性以在文本周围绘制矩形。

    【讨论】:

      【解决方案2】:

      每个TextBlock 元素都包含一个或多个子Inline 元素。有一种内联 (InlineUIContainer) 支持托管 UIElement:基本上,这意味着您可以在 TextBlock 中托管任意 UI 元素。翻转@GraemeF's answer 的头部,您可以在TextBlock 内部 托管Border 元素,如下所示:

      <Window x:Class="StackOverflowWpf.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      
          <TextBlock FontSize="24" Padding="20">
              <Run>Hello</Run>
              <InlineUIContainer>
                  <Border BorderBrush="Black" BorderThickness="1" Padding="4" Margin="0,0,0,-11">
                      <TextBlock Text="Boxed" />
                  </Border>
              </InlineUIContainer>
              <Run>World</Run>
          </TextBlock>
      
      </Window>
      

      这看起来像这样:

      您必须尝试使用​​ Border 元素上的 Margin 以使文本基线匹配,但除此之外,该技术似乎运作良好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多