【问题标题】:How to completely clear/set text of WinRT's RichEditBox?如何完全清除/设置 WinRT 的 RichEditBox 的文本?
【发布时间】:2013-08-30 00:57:28
【问题描述】:

如何完全覆盖或清除 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;
}

Vm_PropertyChanged 只是监听 ViewModel 的 Content 字符串属性的变化,ContentChanged 是 RichEditBox 的 TextChanged 事件的处理程序,将创建一个无限循环,不断将“\r”附加到Vm.Content 和框的文本本身。 当您将 TextGetOptions.None 替换为 TextGetOptions.FormatRtf 时,ViewModel 的 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);
        }
    }

编辑:

一些实验:

        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"

编辑:

另一个实验:

TextGetOptions.None 的一个简单解决方法是修剪输出中多余的“\r”。 然而,有了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

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

【问题讨论】:

    标签: c# .net windows-runtime


    【解决方案1】:

    额外的 /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);
    

    【讨论】:

      【解决方案2】:

      您可以拨打SetText(Windows.UI.Text.TextSetOptions.None, null)。来自SetText的文档:

      如果字符串为NULL,则删除文档中的文本。

      【讨论】:

      • 不幸的是,AgumentNullException:值不能为空。如果文档这么说,这很奇怪。
      • 而且由于文档只涉及 ITextDocument 接口,我们永远不知道实现类会做什么。
      • @MichaelK.Sondej 使用SetText(..., string.Empty) 有用吗?
      • 好吧,如果我只在Vm_PropertyChanged 中执行richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, string.Empty);,它会循环清除该框,因此所有键入的内容都会被清除。这是预期的,但是当我做richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, string.Empty); string content; richEditBox.GetText(Windows.UI.Text.TextGetOptions.None, out content); 时,内容是“\r”。因此,如果我清除该框并尝试将文本设置为不同的内容,它仍然会创建一个无限循环,因为每个连续的 GetText 调用都会将“\r”添加到实际内容中。
      • 因为 GetText 每次都会将该字符添加到内容中,所以 Vm.Content 属性永远不会检测到 Content == value 情况,从而创建了循环。例如,查看已编辑的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多