RichTextBox for UWP | ComponentOne
Working with RichTextBox for UWP / Menus and Commands / Creating Custom Command Bars / Formatting Functions
In This Topic
    Formatting Functions
    In This Topic

    The following code snippets demonstrate the code used for formatting functions:

    Foreground Color

    C#
    Copy Code
    rtb.Selection.Foreground = new SolidColorBrush(Colors.Red);
    

     

    Highlight (background) color

    C#
    Copy Code
    rtb.Selection.InlineBackground = new SolidColorBrush(Colors.Yellow);
    

     

    Toggle Bold

    C#
    Copy Code
    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;