简体   繁体   English

更改 Textblock 控件中特定行的背景颜色

[英]Change Backgroud color of specific line in Textblock control

<TextBlock Text="{Binding Logs,Mode=OneWay}" />

In my UWP application, I want to change the color of lines based on the specific keyword in beginning of the line.在我的 UWP 应用程序中,我想根据行首的特定关键字更改行的颜色。

在此处输入图片说明

I need to change the lines background color if line start with word WARNING如果行以单词WARNING开头,我需要更改行的背景颜色

Can anyone suggest how I can achieve this with code behind?谁能建议我如何通过背后的代码实现这一目标?

I need to change the lines background color if line start with word WARNING如果行以单词 WARNING 开头,我需要更改行的背景颜色

No, you can't change the background color of the line of text directly in the TextBlock Class.不,您不能直接在 TextBlock 类中更改文本行的背景颜色。 This is the same for RichTextBlock Class and RichEditBox Class .这对于RichTextBlock ClassRichEditBox Class来说是一样的。 What you could change is the Foreground color of the text.您可以更改的是文本的前景色。

For your requirement,what you could do is to put the Textblock into a border or something else that has a background property根据您的要求,您可以做的是将 Textblock 放入边框或其他具有背景属性的内容中

Like this:像这样:

<Border Background="Red">
<TextBlock Text="warning ....."
           FontSize="10" />
</Border>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM