Change color and font for some part of text in WPF C#
-
Is there a way to change color and font for some part of text which I want to put on TextBox or RichTextBox. I am using C# WPF. For example richTextBox.AppendText("Text1 " + word + " Text2 "); Variable word for example to be other color and font from Text1 and Text2. Is it possible and how to do this?
-
Answer:
If you just want to do some quick coloring , using the end of the RTB content as a Range and apply formatting to it is maybe the simplest solution, e.g. TextRange rangeOfText1 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); rangeOfText1.Text = "Text1 "; rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); TextRange rangeOfWord = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); rangeOfWord.Text = "word "; rangeOfWord.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red); rangeOfWord.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular); TextRange rangeOfText2 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); rangeOfText2.Text = "Text2 "; rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); If you are looking for a more advanced solution, I suggest reading the MSDN page about the http://msdn.microsoft.com/en-us/library/aa970909.aspx, as this gives you a great flexibility in formatting your text.
Ivan Tanasijevic at Stack Overflow Visit the source
Other answers
You can try out this. public TestWindow() { InitializeComponent(); this.paragraph = new Paragraph(); rich1.Document = new FlowDocument(paragraph); var from = "user1"; var text = "chat message goes here"; paragraph.Inlines.Add(new Bold(new Run(from + ": ")) { Foreground = Brushes.Red }); paragraph.Inlines.Add(text); paragraph.Inlines.Add(new LineBreak()); this.DataContext = this; } private Paragraph paragraph; So use the Document property of the RichTextBox
Rao BHavik
You need to use the Document property of RichTextBox and add a Run to it. Document property: http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.document.aspx Run: http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx
publicgk
Related Q & A:
- How to change color of radio button when checked radio button?Best solution by Stack Overflow
- Why do leaves change color in fall?Best solution by Yahoo! Answers
- What does it mean when your eyes change color?Best solution by Yahoo! Answers
- Why do the leaves change color in the Fall?Best solution by Yahoo! Answers
- How do I change the font size in my mail text?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.