如何完全清除/设置WinRT的RichEditBox文本?

Mic*_*dej 5 .net c# windows-runtime

如何完全覆盖或清除WinRT的RichEditBox的文本(和格式)?

我问,因为它的Document属性的方法SetText似乎只是附加新文本.

因此"绑定"如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

这里Vm_PropertyChanged只是监听的变化Content视图模型的字符串属性,并ContentChanged为处理器TextChanged的RichEditBox的情况下,将创建一个无限循环不断追加"\ r"把Vm.Content和框的文本本身.当您TextGetOptions.None使用TextGetOptions.FormatRtfViewModel 替换Content属性时,会更加混乱,添加看起来像空RTF段落的内容.

这是ViewModel中的Content属性定义,因此您可以确保一切正常:

    /// <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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

一些实验:

        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"
Run Code Online (Sandbox Code Playgroud)

编辑:

另一个实验:

一个简单的解决方法TextGetOptions.None是在输出上修剪额外的"\ r \n".但TextGetOptions.FormatRtf事情并非如此简单:

        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
Run Code Online (Sandbox Code Playgroud)

我为我的英语道歉.有关它的所有更正也欢迎:)

Jon*_*Jon 5

额外的 /r(或 \par,如果您查询 RTF)似乎是 RichEditBox 中的错误。但是,它可以通过执行以下操作来解决:

        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);
Run Code Online (Sandbox Code Playgroud)