Can I color different parts of text in a WPF TextBox?

I've got a WPF TextBox into which my users type XHTML.

XAML...

<TextBox Name="TextBoxContentBody" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" FontFamily="Consolas" ... /> 

It looks like this:

enter image description here

I'd rather that it looked like this (Photoshopped mock-up):

enter image description here

How might I accomplish this?

1

3 Answers

You'll want to use a RichTextBox for that.

Like Kent said with a TextBox you're stuck with one color.

Here is a small article on simple RTB syntax highlighting that should give you most of what you need.

1

You will have to create a custom control inheriting the TextBox control and follow this example:

Drawing Formatted Text

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like