.net 我可以在 WPF 文本框中为文本的不同部分着色吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/583597/
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
Can I color different parts of text in a WPF TextBox?
提问by Zack Peterson
I've got a WPF TextBox into which my users type XHTML.
我有一个 WPF TextBox,我的用户可以在其中键入 XHTML。
XAML...
XAML...
<TextBox Name="TextBoxContentBody"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
FontFamily="Consolas"
... />
It looks like this:
它看起来像这样:


I'd rather that it looked like this (Photoshopped mock-up):
我宁愿它看起来像这样(Photoshop 模型):


How might I accomplish this?
我怎样才能做到这一点?
回答by Kent Boogaart
You'll want to use a RichTextBoxfor that.
为此,您需要使用RichTextBox。
回答by Andrew Barrett
Like Kent said with a TextBox you're stuck with one color.
就像 Kent 用 TextBox 所说的那样,您只能使用一种颜色。
Here is a small article on simple RTB syntax highlighting that should give you most of what you need.
这是一篇关于简单 RTB 语法突出显示的小文章,应该可以满足您的大部分需求。
http://blogs.microsoft.co.il/blogs/tamir/archive/2006/12/14/RichTextBox-syntax-highlighting.aspx
http://blogs.microsoft.co.il/blogs/tamir/archive/2006/12/14/RichTextBox-syntax-highlighting.aspx

