【问题标题】:UWP Underline Textblock with ImageBrush as Foreground does not show the Underline使用 ImageBrush 作为前景的 UWP 下划线文本块不显示下划线
【发布时间】:2017-01-10 03:08:39
【问题描述】:

我使用ImageBrush 作为Textblock Foreground。它工作正常,但是当TextblockUnderline 时,该行不显示。

复制。在 XAML 中

<TextBlock x:Name="textBlock" FontSize="80" FontWeight="Bold">
    <Underline>This is my text</Underline>
</TextBlock>

在后面的代码中

Uri uri = new Uri("ms-appx:///Assets/0.png");
BitmapImage bmp = new BitmapImage(uri);
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = bmp;
this.textBlock.Foreground = imageBrush;

下面是应用了下划线的 SolidColorBrush 的样子

那么当ImageBrush被应用时,下划线就消失了

所以我的问题是如何在 UWP 中将 ImageBrush Foreground 应用于 Underline Textblock

【问题讨论】:

    标签: c# uwp windows-10 textblock underline


    【解决方案1】:

    所以我的问题是如何将 ImageBrush Foreground 应用于 UWP 中的下划线文本块?

    默认情况下,当ImageBrush 应用于TextBlockForeground 属性时,下划线将被移除。

    解决方法是使用Border 模拟下划线:

    <Border BorderThickness="0, 0, 0, 2" Height="{Binding ActualHeight, ElementName=textBlock}" Width="{Binding ActualWidth, ElementName=textBlock}">
                <Border.BorderBrush>
                    <ImageBrush ImageSource="Assets/0.jpg" />
                </Border.BorderBrush>
                <TextBlock x:Name="textBlock" FontSize="50" FontWeight="Bold">
                    <TextBlock.Foreground>
                        <ImageBrush ImageSource="Assets/0.jpg" />
                    </TextBlock.Foreground>
                    <Underline><Run Text="This is my text"/></Underline>
                </TextBlock>
            </Border>
    

    您可以创建一个 UserControl/CustomControl 来保持可重用性。

    【讨论】:

    • 请注意,这不是真正的下划线,也不尊重字形高度和偏移量。这在您的预览中可见,其中下划线被推到“y”中的最低点下方,而不是与其下降线相交。
    【解决方案2】:

    您可以像这样使用 wrappanel 制作内联文本 Inline Texts Image

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多