Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely clear/set text of WinRT's RichEditBox?

How does one completely overwrite or clear the text (and formatting) of WinRT's RichEditBox?

I'm asking because the method SetText of it's Document property seems only to append new text.

Thus the "binding" as below:

void Vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Content")
        richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, Vm.Content);
}

private void ContentChanged(object sender, RoutedEventArgs e)
{
    RichEditBox box = (RichEditBox)sender;

    string content;
    box.Document.GetText(Windows.UI.Text.TextGetOptions.None, out content);

    Vm.Content = content;
}

where Vm_PropertyChanged just listens for changes in the Content string property of the ViewModel and ContentChanged is a handler for TextChanged event of the RichEditBox, will create an infinite loop constantly appending "\r" to the Vm.Content and the box's text itself. When you replace TextGetOptions.None with TextGetOptions.FormatRtf the ViewModel's Content property gets even more messy adding something that looks like empty RTF paragraphs.

Here's the Content property definition in the ViewModel so you can ensure that everything's ok with it:

    /// <summary>
    /// The <see cref="Content" /> property's name.
    /// </summary>
    public const string ContentPropertyName = "Content";

    private string _content;

    /// <summary>
    /// Sets and gets the Content property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public string Content
    {
        get
        {
            return _content;
        }

        set
        {
            if (_content == value)
            {
                return;
            }

            RaisePropertyChanging(ContentPropertyName);
            _content = value;
            RaisePropertyChanged(ContentPropertyName);
        }
    }

EDIT:

Some experiments:

        richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, string.Empty);
        string content;
        richEditBox.Document.GetText(Windows.UI.Text.TextGetOptions.None, out content);
        //content became "\r"

        richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, content);
        richEditBox.Document.GetText(Windows.UI.Text.TextGetOptions.None, out content);
        //content became "\r\r"

EDIT:

Another experiment:

A simple workaround for TextGetOptions.None is trimming that extra "\r" on output. However with TextGetOptions.FormatRtf things aren't just that simple:

        RichEditBox box = new RichEditBox();

        box.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, string.Empty);
        string content;
        box.Document.GetText(Windows.UI.Text.TextGetOptions.FormatRtf, out content);

        //content is now
        // {\\rtf1\\fbidis\\ansi\\ansicpg1250\\deff0\\nouicompat\\deflang1045{\\fonttbl{\\f0\\fnil Segoe UI;}}\r\n{\\colortbl ;\\red255\\green255\\blue255;}\r\n{\\*\\generator Riched20 6.2.9200}\\viewkind4\\uc1 \r\n\\pard\\ltrpar\\tx720\\cf1\\f0\\fs17\\lang1033\\par\r\n}\r\n\0

        box.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, content);
        box.Document.GetText(Windows.UI.Text.TextGetOptions.FormatRtf, out content);

        //and now it's
        // {\\rtf1\\fbidis\\ansi\\ansicpg1250\\deff0\\nouicompat\\deflang1045{\\fonttbl{\\f0\\fnil Segoe UI;}{\\f1\\fnil Segoe UI;}}\r\n{\\colortbl ;\\red255\\green255\\blue255;}\r\n{\\*\\generator Riched20 6.2.9200}\\viewkind4\\uc1 \r\n\\pard\\ltrpar\\tx720\\cf1\\f0\\fs17\\lang1033\\par\r\n\r\n\\pard\\ltrpar\\tx720\\f1\\fs17\\par\r\n}\r\n\0

I apologize for my English. All corrections concerning it are also welcome :)

like image 565
Michael K. Sondej Avatar asked Aug 19 '13 17:08

Michael K. Sondej


1 Answers

The extra /r (or \par if you query for RTF) appears to be a bug in the RichEditBox. It can be worked around however, by doing something like this:

        string temp;
        // Do not ask for RTF here, we just want the raw text
        richEditBox.Document.GetText(TextGetOptions.None, out temp);
        var range = richEditBox.Document.GetRange(0, temp.Length - 1);

        string content;
        // Ask for RTF here, if desired.
        range.GetText(TextGetOptions.FormatRtf, out content);
like image 158
Jon Avatar answered Nov 19 '22 08:11

Jon