Working with WPF RichTextBox / Custom Command Bars / Formatting Functions
Formatting Functions

The following code snippets demonstrate the code used for formatting functions in RichTextBox .NET and .NET Framework versions.

You can also use Ctrl+a shortcut to select the entire text, Ctrl+b shortcut to bold the text, Ctrl+i shortcut to italicize the text, and Ctrl+u shortcut to underline the text.  

Foreground Color

C#
Copy Code
rtb.Selection.Foreground = newSolidColorBrush(Colors.Red);
Highlight (background) color
rtb.Selection.InlineBackground = newSolidColorBrush(Colors.Yellow);
Toggle Bold
if(rtb.Selection.FontWeight != null&& rtb.Selection.FontWeight.Value.Weight == FontWeights.Bold.Weight)
{
    rtb.Selection.FontWeight = FontWeights.Normal;
}
else
{
    rtb.Selection.FontWeight = FontWeights.Bold;
}

Toggle Italic

C#
Copy Code
if(rtb.Selection.FontStyle != null&& rtb.Selection.FontStyle == FontStyle.Italic)
{
    rtb.Selection.FontStyle = FontStyle.Normal;
}
else
{
    rtb.Selection.FontStyle = FontStyle.Italic;
}

Toggle Underline

C#
Copy Code
var range = rtb.Selection;
var collection = new C1TextDecorationCollection();
if (range.TextDecorations == null)
{
    collection.Add(C1TextDecorations.Underline[0]);
}
else if (!range.TextDecorations.Contains(C1TextDecorations.Underline[0]))
{
    foreach (var decoration in range.TextDecorations)
        collection.Add(decoration);

    collection.Add(C1TextDecorations.Underline[0]);
}
else
{
    foreach (var decoration in range.TextDecorations)
        collection.Add(decoration);

    collection.Remove(C1TextDecorations.Underline[0]);
    if (collection.Count == 0)
        collection = null;
}
range.TextDecorations = collection;

Clear Formatting

C#
Copy Code
rtb.Selection.InlineBackground = null;
rtb.Selection.Foreground = rtb.Foreground;
rtb.Selection.FontWeight = FontWeights.Normal;
rtb.Selection.FontStyle = FontStyle.Normal;
rtb.Selection.TextDecorations = null;