2013-08-19 33 views
5

如何完全覆蓋或清除WinRT RichEditBox的文本(和格式)?如何完全清除/設置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只是監聽在視圖模型的Content字符串屬性和ContentChanged變化是TextChanged事件RichEditBox的處理程序,將創建一個無限循環不斷將「\ r」附加到Vm.Content和框的文本本身。 當您將TextGetOptions.None替換爲TextGetOptions.FormatRtf時,ViewModel的Content屬性會變得更加混亂,從而添加看起來像空RTF段落的東西。

下面是視圖模型的內容屬性定義,所以你可以確保一切正常吧:

/// <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 

我爲我的英語道歉。所有關於它的更正也歡迎:)

回答

4

extra/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); 
+0

偉大的解決方案,按預期工作。 –

0

您可以致電SetText(Windows.UI.Text.TextSetOptions.None, null)。從SetText的文檔:

如果字符串爲NULL,文檔中的文本將被刪除。

+0

不幸的是,AgumentNullException:值不能爲空。如果文檔中這樣說,這很奇怪。 –

+0

由於文檔僅涉及ITextDocument接口,因此我們永遠不知道實現的類將執行什麼操作。 –

+0

@ MichaelK.Sondej是否使用'SetText(...,string.Empty)'工作? –