Change color and font for some part of text in WPF C#

wpf textblock color part of text
wpf richtextbox add text with color
wpf richtextbox text color
wpf richtextbox multiple colors
wpf textbox text color
wpf textblock indent text
wpf display formatted text
richtextbox change color 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?

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 FlowDocument, as this gives you a great flexibility in formatting your text.

Change font color of part of text present in TextBox wpf C#, I have a TextBox in WPF application. Example the Text is "Hello" with foreground color as Black, now if I type "World" in the same textbox I need to� Questions: 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.

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

How to Change Word Font Color in WPF, This article describes how to change word font color with Spire.Doc for WPF in C#, VB.NET. At first, please 07, //Set Text Color for Paragraph 1(the Title)� I need change color in code behind for part of text string.. My exaple: Change color and font for some part of text in WPF C#. Related. 597.

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

[C# & WPF] Changing color of part of text in RichTextBox, I am new to C# with WPF and I am having trouble with a task that I thought com/ questions/5442067/change-color-and-font-for-some-part-of-text-in-wpf-c-sharp. If you use TextRange(tbScriptCode.Document.ContentStart, tbScriptCode.Document.ContentEnd); all existing text will be overwritten. Play a bit with all this stuff. I did this myself some weeks ago, where I searched for a quick way to display some text with specific lines displayed in color. I ended with the FlowDocument solution.

I have made my own class to manipulate Texts of TextBlock, TextBox...

/// <summary>
/// Class for text manipulation operations
/// </summary>
public class TextManipulation
{
    /// <summary>
    /// Is manipulating a specific string inside of a TextPointer Range (TextBlock, TextBox...)
    /// </summary>
    /// <param name="startPointer">Starting point where to look</param>
    /// <param name="endPointer">Endpoint where to look</param>
    /// <param name="keyword">This is the string you want to manipulate</param>
    /// <param name="fontStyle">The new FontStyle</param>
    /// <param name="fontWeight">The new FontWeight</param>
    /// <param name="foreground">The new foreground</param>
    /// <param name="background">The new background</param>
    /// <param name="fontSize">The new FontSize</param>
    public static void FromTextPointer(TextPointer startPointer, TextPointer endPointer, string keyword, FontStyle fontStyle, FontWeight fontWeight, Brush foreground, Brush background, double fontSize)
    {
        FromTextPointer(startPointer, endPointer, keyword, fontStyle, fontWeight, foreground, background, fontSize, null);
    }

    /// <summary>
    /// Is manipulating a specific string inside of a TextPointer Range (TextBlock, TextBox...)
    /// </summary>
    /// <param name="startPointer">Starting point where to look</param>
    /// <param name="endPointer">Endpoint where to look</param>
    /// <param name="keyword">This is the string you want to manipulate</param>
    /// <param name="fontStyle">The new FontStyle</param>
    /// <param name="fontWeight">The new FontWeight</param>
    /// <param name="foreground">The new foreground</param>
    /// <param name="background">The new background</param>
    /// <param name="fontSize">The new FontSize</param>
    /// <param name="newString">The New String (if you want to replace, can be null)</param>
    public static void FromTextPointer(TextPointer startPointer, TextPointer endPointer, string keyword, FontStyle fontStyle, FontWeight fontWeight, Brush foreground, Brush background, double fontSize, string newString)
    {
        if(startPointer == null)throw new ArgumentNullException(nameof(startPointer));
        if(endPointer == null)throw new ArgumentNullException(nameof(endPointer));
        if(string.IsNullOrEmpty(keyword))throw new ArgumentNullException(keyword);

        TextRange text = new TextRange(startPointer, endPointer);
        TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
        while (current != null)
        {
            string textInRun = current.GetTextInRun(LogicalDirection.Forward);
            if (!string.IsNullOrWhiteSpace(textInRun))
            {
                int index = textInRun.IndexOf(keyword);
                if (index != -1)
                {
                    TextPointer selectionStart = current.GetPositionAtOffset(index,LogicalDirection.Forward);
                    TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length,LogicalDirection.Forward);
                    TextRange selection = new TextRange(selectionStart, selectionEnd);

                    if(!string.IsNullOrEmpty(newString))
                        selection.Text = newString;

                    selection.ApplyPropertyValue(TextElement.FontSizeProperty, fontSize);
                    selection.ApplyPropertyValue(TextElement.FontStyleProperty, fontStyle);
                    selection.ApplyPropertyValue(TextElement.FontWeightProperty, fontWeight);
                    selection.ApplyPropertyValue(TextElement.ForegroundProperty, foreground);
                    selection.ApplyPropertyValue(TextElement.BackgroundProperty, background);
                }
            }
            current = current.GetNextContextPosition(LogicalDirection.Forward);
        }
    }
}
Usage
TextManipulation.FromTextPointer(_TextBlock.ContentStart, _TextBlock.ContentEnd, "IWantToBeManipulated", NewFontStyle, NewFontWeight, NewForeground, NewBackground, NewFontSize);
TextManipulation.FromTextPointer(_TextBlock.ContentStart, _TextBlock.ContentEnd, "IWantToBeManipulated", NewFontStyle, NewFontWeight, NewForeground, NewBackground, NewFontSize, "NewStringIfYouWant");

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. Stack OverflowColor Change. While some might be useful, most are spam and some are wrong or misleading as in this case. This was brought to the top of the list by two "Not an answer" which has been removed meanwhile. They were similar to solution 2 and 3 which do not recognised that the question is for a C# desktop application. So allways check the date(s) before answering.

The TextBlock control - Inline formatting, We even used another color than the default for rendering the text, but what if you class, which means that they can be rendered inline, as a part of a larger text. the underlying Span element, the Italic element sets the FontStyle and so on. As you can see, formatting text through XAML is very easy, but in some cases,� This is a video tutorial about Changing Color of Specific Text in RichTextBox in C#/CSharp. How to Change Font Colour And Font Size in c# At Run Time - Duration: 6:29.

wpf Knowledge Base, 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. To change color characteristics for a range of cells, call the CellRange.BeginUpdateFormatting method for this range, modify the Font and Fill properties of the returned Formatting object and call the CellRange.EndUpdateFormatting method to finalize the modification.

How to Change Color of Specific Text in RichTextBox in C#/CSharp, This is a video tutorial about Changing Color of Specific Text in RichTextBox in C#/CSharp.Duration: 3:44 Posted: 21 Jun 2016 C# WPF change colour with text. the thing is the XAML engine can recognize and implicitly convert it into Color. It goes the same for specifying the source of the

Comments
  • After the object "rangeOfText1", "rangeOfWord" and "rangeOfText2" are complete, how concat them?
  • Thank you, this is the simplest way I can find!