WPF文本框更改文本的背景颜色,而不是全部文本框
我想知道是否有一种方法可以更改textbox
内文本背景的颜色,而不是整个textbox
。 就像当您突出显示/选择文本时一样。
I would like to know if there is a way to change the color of the background of the text inside a TextBox
and not for the whole TextBox
.
Like when you highlight/select the text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文本框
通常不支持彩色文本或丰富的文本格式。根据您的方案,您必须使用textblock
或richtextbox
。TextBlock
您可以直接处理文本元素:
或者,如果您需要找到特定的文本,请处理文本指针:
要使“突出显示动态”,您可以使用
MultiBinding
使用文本来创建内联元素 - 到内线转换器。由于我们无法直接绑定到textblock.inlines
属性,因此我们使用textblock.text.text
属性作为虚拟绑定目标:rightlightighlightinfo.cs
texttoinlineconverter.cs
mainwindow.xaml.cs
mainwindow.xaml.xaml
richtextbox
如果您需要允许用户输入,则必须使用a <<代码> RichTextbox :
TextBox
doesn't support coloured text or rich text formatting in general. Depending on your scenario, you would have to go with aTextBlock
orRichtextBox
.TextBlock
You can either handle the text elements directly:
Or in case you need to find a particular text, handle the text pointers:
To make highlighting dynamic you can use a
MultiBinding
to create the inline elements using a text-to-Inline converter. Since we can't bind directly to theTextBlock.Inlines
property, we use theTextBlock.Text
property as a dummy binding target:HighlightInfo.cs
TextToInlineConverter.cs
MainWindow.xaml.cs
MainWindow.xaml
RichtextBox
In case you need to allow user input, you would have to use a
RichTextBox
: