Question

I have a RichTextBox with bolded text, but need to programmatically cause the bolding to "end" when the user enters a semicolon ;.

For instance, my rich text box has the following boldedtext:

hello world

and when I add a semicolon ;, and continue typing, I would like something along the lines of the following:

hello world;how are you?

I've tried something along the lines of:

var newRun = new Run();
paragraph.Inlines.InsertAfter(paragraph.Inlines.LastInline, newRun);
CaretPosition = newRun.ElementEnd;

but the newly created run seems to not be the one I'm typing into (I don't know if it just doesn't exist, or I'm just not typing into it).

Does anybody know a good way around this problem?

Update: Works with a run with default text.

I've noticed that if I create a run:

var newRun = new Run(" ")

then it will work, but there are cases in which I would prefer to not add a space. Any insight?

Was it helpful?

Solution

Try changing the line :

CaretPosition = newRun.ElementEnd;

To:

CaretPosition = newRun.ContentEnd;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top