如何更改 WPF TextBlock 中的 TextDecoration 颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32587205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:25:03  来源:igfitidea点击:

How to change TextDecoration color in WPF TextBlock?

c#wpftextblocktext-decorations

提问by Dinesh balan

I am changing the color of the TextDecorationthis way:

我正在改变TextDecoration这种方式的颜色:

<Grid Background="{x:Null}"
      Margin="10,0,10,0">
    <TextBlock Text="{Binding Value}"
               VerticalAlignment="Center"
               HorizontalAlignment="Center"
               Style="{StaticResource SWMRegularTextBlockStyle}"
               Margin="0"
               FontSize="{DynamicResource RegularFontSize}"
               x:Name="tb" />
        <Line VerticalAlignment="Center"
              HorizontalAlignment="Center"
              Visibility="{Binding InStock, Converter={StaticResource ReverseBooleanToVisiblity}}"
              Stroke="Red"
              Margin="0"
              StrokeThickness="2"
              X1="1"
              Stretch="Fill"
              Width="{Binding ActualWidth, ElementName=tb, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

But when Texthas two lines it fails. Please help me to change the color of TextDecoration. Thanks in advance.

但是当Text有两条线时它会失败。请帮我更改TextDecoration的颜色。提前致谢。

NOTE: I want TextBlockforeground and strike-through line in different colors.

注意:我想要TextBlock不同颜色的前景线和删除线。

回答by Alias Varghese

I think this is what you are looking for.

我想这就是你要找的。

<TextBlock Text="{Binding Value}" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource  SWMRegularTextBlockStyle}" Margin="0" FontSize="{DynamicResource RegularFontSize}" x:Name="tb" >
   <TextBlock.TextDecorations>
        <TextDecoration Location="Strikethrough">
            <TextDecoration.Pen>
                <Pen Brush="Red" />
            </TextDecoration.Pen>
        </TextDecoration>
    </TextBlock.TextDecorations>
</TextBlock>

回答by timbo

The problem you have is that you're overlaying a line on the text. When the text wraps you need to create another line which is not going to be easy.

您遇到的问题是您在文本上覆盖了一行。当文本换行时,您需要创建另一行,这并不容易。

You can solve this by not using the line at all but instead using a specific pen for the TextDecoration of the strikethrough in the code behind.

您可以通过根本不使用线条来解决这个问题,而是在后面的代码中使用特定的笔作为删除线的 TextDecoration。

Answer found here

答案在这里找到

    private void WindowLoaded(object sender, EventArgs e)
    {
        // Fill the overline decoration with a solid color brush.
        TextDecorationCollection myCollection = new TextDecorationCollection();
        TextDecoration myStrikeThrough = new TextDecoration();
        myStrikeThrough.Location = TextDecorationLocation.Strikethrough;

        // Set the solid color brush.
        myStrikeThrough.Pen = new Pen(Brushes.Red, 2);
        myStrikeThrough.PenThicknessUnit = TextDecorationUnit.FontRecommended;

        // Set the underline decoration to the text block.
        myCollection.Add(myStrikeThrough);
        tb.TextDecorations = myCollection;
    }

And then simplify your XAML. Remove the Line control, and add Loaded="WindowLoaded"to your Window

然后简化您的 XAML。删除 Line 控件,并添加Loaded="WindowLoaded"到您的窗口