Click here to Skip to main content
12,253,114 members (46,549 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

10.6K views
1 bookmarked
Posted

WPF RichTextBox features in TextBlock

, 28 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
In WPF, the TextBlock is used to display simple text. We can apply several formatting to the text like Bold, Italics or Underline to name a few.But it is a little known fact that it is possible to display several differently formatted text in a SINGLE TextBlock!!!for e.g. you can make a...
In WPF, the TextBlock is used to display simple text. We can apply several formatting to the text like Bold, Italics or Underline to name a few.

But it is a little known fact that it is possible to display several differently formatted text in a SINGLE TextBlock!!!
for e.g. you can make a TextBlock display the following text

This is a simple multiformat text!


This can be achieved by using multiple Run.

Normally when you assign a text to the TextBlock, it creates a single Run which encapsulates the text assigned.

In order to add multiformatted text to a TextBlock, you have to create several Runs and add them to the TextBlock's Inlines property.

Here is an example

Run run1 = new Run("This is ");
 
Run run2 = new Run("bold");
run2.FontWeight = FontWeights.Bold;
 
Run run3 = new Run(" and ");
 
Run run4 = new Run("italic");
run4.FontStyle = FontStyles.Italic;
 
Run run5 = new Run("text.");
 
myTextBlock.Inlines.Add(run1);
myTextBlock.Inlines.Add(run2);
myTextBlock.Inlines.Add(run3);
myTextBlock.Inlines.Add(run4);
myTextBlock.Inlines.Add(run5);
This will set the myTextBlock as

This is bold and italic text.

More information on TextBlock can be found here[^].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ratish Philip
Software Developer
United States United States
An individual with more than a decade of experience in desktop computing and mobile app development primarily on the Microsoft platform. He loves programming in C#, WPF & XAML related technologies.
Current interests include web application development, developing rich user experiences across various platforms and exploring his creative side.

Ratish's personal blog: wpfspark.wordpress.com

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 1 Mar 2010
Article Copyright 2010 by Ratish Philip
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid