Reputation: 381
I'm trying to make my WPF RichTextBox appear like a coding page.
I marked some words to determind it's a keyword or a string,... Now I have text like this:
<@$keyword>int<keyword$@> sum;
sum = 1;
Console.WriteLine(<@$string>"{0}"<string$@>,sum);
The result should be like this:
int sum;
sum = 1;
Console.WriteLine("{0}",sum);
But "int" is blue and "{0}" is pink.
Here is my xaml RichTextBox code:
<RichTextBox x:Name="richTextBox"
VerticalScrollBarVisibility="Auto">
<FlowDocument>
<Paragraph>
<Run Text="{Binding codeContent}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
My question refer to this stackoverflow question but it not seem to solve my problem.
Because my WPF RichTextBox built up with FlowDocument inside, we can't get the content text by things like String st = myRichtextBox.Text
, we need to use TextRange to get the text inside it: TextRange tr = new TextRange(rtb.Document.ContentStart,rtb.Document.ContentEnd)
.
But another problem is it get the whole text, not get a part of content text that I want. Example: RichTextBox rtb contain:<@$keyword>int<keyword$@> sum;sum = 1;Console.WriteLine(<@$string>"{0}"<string$@>,sum);
and I want to get just <@$keyword>int<keyword$@>
and <@$string>"{0}"<string$@>
and edit it not the whole text.
Upvotes: 0
Views: 1834
Reputation: 1426
TextRange tr = new TextRange(rtb.Document.ContentStart,rtb.Document.ContentEnd)
tr.Text gives you the plain string of the contents. Now you have an string to use with regex/substrings following:
How to select text from the RichTextBox and then color it?
Upvotes: 1