Skip to content

How to use RichTextBox in WPF? #7596

Answered by miloush
VahidEra asked this question in Q&A
Mar 5, 2023 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

To change a color of a range, for example selection, you can use ApplyPropertyValue:

_rtb.Selection.ApplyPropertyValue(TextBlock.ForegroundProperty, Brushes.Red);

To get a text of RTB, you need to create a text range first and then you can get or set text:

string text = new TextRange(_rtb.Document.ContentStart, _rtb.Document.ContentEnd).Text;

E.g. to insert a new line, at the selection start, you create a range and set text in it:

TextPointer selectionStart = _rtb.Selection.Start;
new TextRange(selectionStart, selectionStart).Text = Environment.NewLine;

to count lines, you either have to count lines in the text retrieved as above (e.g. splitting the string by new line into an array), or y…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@miloush
Comment options

miloush Mar 5, 2023
Collaborator

@VahidEra
Comment options

Answer selected by VahidEra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants