RichTextBox control in WPF
RichTextBox:
Namespace : System.Windows.Controls.RichTextBox
RichTextBox may contain any combination of
• Unicode text.
• UIElement. Image, a nested Text, etc.
• ContentElement. LineBreak, etc.
• TextElement. Bold, Paragraph, etc – content that spans other content. TextElements like Bold (anything that derives from Inline) are essentially platforms for DependencyProperties applied to spanned content. TextElements like Paragraph (things that derive from BlockElement) may hold property values, but always provide structural information used by the layout engine.
The RichTextBox operates on top of a Document object, which allows you to manipulate the text in far more powerful ways than what was exposed in Windows Forms.
A flow document is designed to "reflow content" depending on window size, device resolution, and other environment variables. In addition, flow documents have a number of built in features including search, viewing modes that optimize readability, and the ability to change the size and appearance of fonts.
Designer Code:
<RichTextBox>
<FlowDocument>
<Paragraph>Text in paragraph one.</Paragraph>
<Paragraph>Text in paragraph two.</Paragraph>
</FlowDocument>
</RichTextBox>
<RichTextBox Margin="10,10,0,0" Name="RichTextBox1"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="184"
Height="215" TextChanged="RichTextBox1_TextChanged">
<FlowDocument>
<Paragraph>
<Bold>Text in Paragraph one</Bold>
</Paragraph>
<<Paragraph Foreground="Red">
Text in Paragraph Two.
</Paragraph>
</FlowDocument>
</RichTextBox>
Code Behind:
// Window1.xaml.cs
private void RichTextBox1_TextChanged(object sender,
TextChangedEventArgs e)
{
TextRange textRange = new
TextRange(RichTextBox1.Document.ContentStart,
RichTextBox1.Document.ContentEnd);
// MessageBox.Show(textRange.Text);
}
//Adding text from code
FlowDocument fldoc = new FlowDocument();
// Create a paragraph with text
Paragraph para1 = new Paragraph();
para1.Inlines.Add(new Bold(new Run("sampletext")));
fldoc.Blocks.Add(para1);
RichTextBox1.Document = fldoc;
The RichTextBox operates on top of a Document object, which allows you to manipulate the text in far more powerful ways than what was exposed in Windows Forms.
That said it’s more difficult to work with. To load in text from a file:
Loading Documents in RichTextBox:
RichTextBox rtb = new RichTextBox();
string text = File.OpenText(@"c:\temp\sample.txt").ReadToEnd();
rtb.Document.Blocks.Clear(); //if you want to clear before adding text
rtb.Document.Blocks.Add(new Paragraph(new Run(text)));
Sample2:
TextRange range;
System.IO.FileStream fStream;
if (System.IO.File.Exists(fileName))
{
range = new TextRange(RichTextBox1.Document.ContentStart,
RichTextBox1.Document.ContentEnd);
fStream = new System.IO.FileStream(fileName,
System.IO.FileMode.OpenOrCreate);
range.Load(fStream, System.Windows.DataFormats.Text );
fStream.Close();
}
Spell checking functionality of RichTextBox :
Supports only four languages(English, Spanish, German and French), have to specify the language.
<RichTextBox SpellCheck.IsEnabled="True" Language="en-US" />
UI controls in RichTextBox :
<BlockUIContainer>
<Button>Button1!</Button>
</BlockUIContainer>
Strikethrough text in RichTextBox :
<Paragraph TextDecorations="Strikethrough">
Text to Strikethrough.
</Paragraph>