Home
%3CLINGO-SUB%20id%3D%22lingo-sub-853791%22%20slang%3D%22en-US%22%3EWhen%20can%20UWP's%20RichEditBox%20control%20be%20available%20to%20set%20RTF%20correctly%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-853791%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20generally%2C%20we%20are%20defining%20RichEditText%20at%20a%20XAML%20file%20like%20following%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3CPAGE%20class%3D%22RTFTestApp.MainPage%22%20xmlns%3D%22%26lt%3BA%20href%3D%22%3Ehttp%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%2Fpresentation%22%0A%20%20%20%20xmlns%3Ax%3D%22%3CA%20href%3D%22http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%3C%2FA%3E%22%0A%20%20%20%20xmlns%3Alocal%3D%22using%3ARTFTestApp%22%0A%20%20%20%20xmlns%3Ad%3D%22%3CA%20href%3D%22http%3A%2F%2Fschemas.microsoft.com%2Fexpression%2Fblend%2F2008%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fschemas.microsoft.com%2Fexpression%2Fblend%2F2008%3C%2FA%3E%22%0A%20%20%20%20xmlns%3Amc%3D%22%3CA%20href%3D%22http%3A%2F%2Fschemas.openxmlformats.org%2Fmarkup-compatibility%2F2006%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fschemas.openxmlformats.org%2Fmarkup-compatibility%2F2006%3C%2FA%3E%22%0A%20%20%20%20mc%3AIgnorable%3D%22d%22%0A%20%20%20%20Loaded%3D%22Page_Loaded%22%0A%20%20%20%20Background%3D%22%7BThemeResource%20ApplicationPageBackgroundThemeBrush%7D%22%26gt%3B%0A%0A%20%20%20%20%3CGRID%20name%3D%22grid%22%3E%0A%20%20%20%20%20%20%20%20%3CRICHEDITBOX%20name%3D%22richEditBox%22%3E%3C%2FRICHEDITBOX%3E%0A%20%20%20%20%3C%2FGRID%3E%0A%3C%2FPAGE%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIn%20this%20case%2C%20we%20can%20set%20RTF%20everywhere%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20Windows.UI.Text%3B%0Ausing%20Windows.UI.Xaml%3B%0Ausing%20Windows.UI.Xaml.Controls%3B%0A%0A%0Anamespace%20RTFTestApp%0A%7B%0A%20%20%20%20public%20sealed%20partial%20class%20MainPage%20%3A%20Page%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20MainPage()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20this.InitializeComponent()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20void%20Page_Loaded(object%20sender%2C%20RoutedEventArgs%20e)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20rtf%20%3D%20%40%22%7B%5Crtf1%5Cansi%5Cansicpg932%5Cdeff0%5Cnouicompat%5Cdeflang1033%5Cdeflangfe1041%7B%5Cfonttbl%7B%5Cf0%5Cfnil%5Cfcharset0%20Matura%20MT%20Script%20Capitals%3B%7D%7B%5Cf1%5Cfnil%5Cfcharset128%20%5C'82%5C'6c%5C'82%5C'72%20%5C'96%5C'be%5C'92%5C'a9%3B%7D%7D%0A%7B%5Ccolortbl%20%3B%5Cred255%5Cgreen0%5Cblue0%3B%7D%0A%7B%5C*%5Cgenerator%20Riched20%2010.0.18362%7D%5Cviewkind4%5Cuc1%20%0A%5Cpard%5Csa200%5Csl276%5Cslmult1%5Cf0%5Cfs22%5Clang17%20This%20is%20a%20%5Ccf1%5Cfs40%20sample%20%5Ccf0%5Cfs22%20RTF%5Cf1%5Cpar%0A%7D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20richEditBox.Document.SetText(TextSetOptions.FormatRtf%2C%20rtf)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EA%20result%20of%20above%20code%20is%20as%20below%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131613iC9D4103679A2C811%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_1.png%22%20title%3D%22clipboard_image_1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIt%20works%20fine.%20However%2C%20if%20creating%20RichEditBox%20in%20C%23%20code%2C%20sometime%20it%20doesn't%20work.%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20removing%20the%20RichEditBox%20tag%20from%20the%20XAML%20file%2C%20and%20editing%20Loaded%20event%20handler%20like%20below%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20Windows.UI.Text%3B%0Ausing%20Windows.UI.Xaml%3B%0Ausing%20Windows.UI.Xaml.Controls%3B%0A%0A%0Anamespace%20RTFTestApp%0A%7B%0A%20%20%20%20public%20sealed%20partial%20class%20MainPage%20%3A%20Page%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20MainPage()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20this.InitializeComponent()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20void%20Page_Loaded(object%20sender%2C%20RoutedEventArgs%20e)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20richEditBox%20%3D%20new%20RichEditBox()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20grid.Children.Add(richEditBox)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20rtf%20%3D%20%40%22%7B%5Crtf1%5Cansi%5Cansicpg932%5Cdeff0%5Cnouicompat%5Cdeflang1033%5Cdeflangfe1041%7B%5Cfonttbl%7B%5Cf0%5Cfnil%5Cfcharset0%20Matura%20MT%20Script%20Capitals%3B%7D%7B%5Cf1%5Cfnil%5Cfcharset128%20%5C'82%5C'6c%5C'82%5C'72%20%5C'96%5C'be%5C'92%5C'a9%3B%7D%7D%0A%7B%5Ccolortbl%20%3B%5Cred255%5Cgreen0%5Cblue0%3B%7D%0A%7B%5C*%5Cgenerator%20Riched20%2010.0.18362%7D%5Cviewkind4%5Cuc1%20%0A%5Cpard%5Csa200%5Csl276%5Cslmult1%5Cf0%5Cfs22%5Clang17%20This%20is%20a%20%5Ccf1%5Cfs40%20sample%20%5Ccf0%5Cfs22%20RTF%5Cf1%5Cpar%0A%7D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20richEditBox.Document.SetText(TextSetOptions.FormatRtf%2C%20rtf)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIt%20is%20just%20changed%20to%20create%20RichEditBox%20on%20C%23%20code.%20It%20doesn't%20work%20correctly.%20Font%20doesn't%20set.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131614i46790AD870AFC26F%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_2.png%22%20title%3D%22clipboard_image_2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1821070041%22%20id%3D%22toc-hId-1821070042%22%3EHow%20to%20fix%20it%3F%3C%2FH2%3E%0A%3CP%3EThe%20issue%20is%20fixed%20just%20calling%20UpdateLayout%20method%20before%20call%20SetText.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20Windows.UI.Text%3B%0Ausing%20Windows.UI.Xaml%3B%0Ausing%20Windows.UI.Xaml.Controls%3B%0A%0A%0Anamespace%20RTFTestApp%0A%7B%0A%20%20%20%20public%20sealed%20partial%20class%20MainPage%20%3A%20Page%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20MainPage()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20this.InitializeComponent()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20void%20Page_Loaded(object%20sender%2C%20RoutedEventArgs%20e)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20richEditBox%20%3D%20new%20RichEditBox()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20grid.Children.Add(richEditBox)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20grid.UpdateLayout()%3B%20%2F%2F%20add%20this%20line%20before%20call%20SetText.%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20rtf%20%3D%20%40%22%7B%5Crtf1%5Cansi%5Cansicpg932%5Cdeff0%5Cnouicompat%5Cdeflang1033%5Cdeflangfe1041%7B%5Cfonttbl%7B%5Cf0%5Cfnil%5Cfcharset0%20Matura%20MT%20Script%20Capitals%3B%7D%7B%5Cf1%5Cfnil%5Cfcharset128%20%5C'82%5C'6c%5C'82%5C'72%20%5C'96%5C'be%5C'92%5C'a9%3B%7D%7D%0A%7B%5Ccolortbl%20%3B%5Cred255%5Cgreen0%5Cblue0%3B%7D%0A%7B%5C*%5Cgenerator%20Riched20%2010.0.18362%7D%5Cviewkind4%5Cuc1%20%0A%5Cpard%5Csa200%5Csl276%5Cslmult1%5Cf0%5Cfs22%5Clang17%20This%20is%20a%20%5Ccf1%5Cfs40%20sample%20%5Ccf0%5Cfs22%20RTF%5Cf1%5Cpar%0A%7D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20richEditBox.Document.SetText(TextSetOptions.FormatRtf%2C%20rtf)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20result%20is%20as%20below%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131615iF20CA6CC023D55DB%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_3.png%22%20title%3D%22clipboard_image_3.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThis%20issue%20is%20usually%20occurred%20when%20implements%20printing%20feature.%20Because%20sometimes%2C%20controls%20are%20created%20in%20C%23%20code%20for%20print%20preview%20and%20printing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20hope%20this%20article%20will%20help%20someone.%3C%2FP%3E%0A%3CP%3EHappy%20coding!%3C%2FP%3E%3C%2FLINGO-BODY%3E
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!