When can UWP's RichEditBox control be available to set RTF correctly?
Published Sep 12 2019 09:27 PM 3,136 Views
Microsoft

In generally, we are defining RichEditText at a XAML file like following:

<Page
    x:Class="RTFTestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:RTFTestApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Loaded="Page_Loaded"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid x:Name="grid">
        <RichEditBox x:Name="richEditBox" />
    </Grid>
</Page>

In this case, we can set RTF everywhere:

using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


namespace RTFTestApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var rtf = @"{\rtf1\ansi\ansicpg932\deff0\nouicompat\deflang1033\deflangfe1041{\fonttbl{\f0\fnil\fcharset0 Matura MT Script Capitals;}{\f1\fnil\fcharset128 \'82\'6c\'82\'72 \'96\'be\'92\'a9;}}
{\colortbl ;\red255\green0\blue0;}
{\*\generator Riched20 10.0.18362}\viewkind4\uc1 
\pard\sa200\sl276\slmult1\f0\fs22\lang17 This is a \cf1\fs40 sample \cf0\fs22 RTF\f1\par
}";
            richEditBox.Document.SetText(TextSetOptions.FormatRtf, rtf);
        }
    }
}

A result of above code is as below:

clipboard_image_1.png

It works fine. However, if creating RichEditBox in C# code, sometime it doesn't work.

For example, removing the RichEditBox tag from the XAML file, and editing Loaded event handler like below:

using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


namespace RTFTestApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var richEditBox = new RichEditBox();
            grid.Children.Add(richEditBox);
            var rtf = @"{\rtf1\ansi\ansicpg932\deff0\nouicompat\deflang1033\deflangfe1041{\fonttbl{\f0\fnil\fcharset0 Matura MT Script Capitals;}{\f1\fnil\fcharset128 \'82\'6c\'82\'72 \'96\'be\'92\'a9;}}
{\colortbl ;\red255\green0\blue0;}
{\*\generator Riched20 10.0.18362}\viewkind4\uc1 
\pard\sa200\sl276\slmult1\f0\fs22\lang17 This is a \cf1\fs40 sample \cf0\fs22 RTF\f1\par
}";
            richEditBox.Document.SetText(TextSetOptions.FormatRtf, rtf);
        }
    }
}

It is just changed to create RichEditBox on C# code. It doesn't work correctly. Font doesn't set.

clipboard_image_2.png

How to fix it?

The issue is fixed just calling UpdateLayout method before call SetText.

using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


namespace RTFTestApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var richEditBox = new RichEditBox();
            grid.Children.Add(richEditBox);
            grid.UpdateLayout(); // add this line before call SetText.
            var rtf = @"{\rtf1\ansi\ansicpg932\deff0\nouicompat\deflang1033\deflangfe1041{\fonttbl{\f0\fnil\fcharset0 Matura MT Script Capitals;}{\f1\fnil\fcharset128 \'82\'6c\'82\'72 \'96\'be\'92\'a9;}}
{\colortbl ;\red255\green0\blue0;}
{\*\generator Riched20 10.0.18362}\viewkind4\uc1 
\pard\sa200\sl276\slmult1\f0\fs22\lang17 This is a \cf1\fs40 sample \cf0\fs22 RTF\f1\par
}";
            richEditBox.Document.SetText(TextSetOptions.FormatRtf, rtf);
        }
    }
}

The result is as below:

clipboard_image_3.png

This issue is usually occurred when implements printing feature. Because sometimes, controls are created in C# code for print preview and printing.

 

I hope this article will help someone.

Happy coding!

Version history
Last update:
‎Sep 12 2019 09:27 PM
Updated by: