Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to completely clear/set text of WinRT's RichEditBox?
    primarykey
    data
    text
    <p>How does one completely overwrite or clear the text (and formatting) of WinRT's RichEditBox?</p> <p>I'm asking because the method <a href="http://msdn.microsoft.com/pl-pl/library/windows/apps/windows.ui.text.itextdocument.settext" rel="nofollow">SetText</a> of it's Document property seems only to append new text.</p> <p>Thus the "binding" as below:</p> <pre><code>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; } </code></pre> <p>where <code>Vm_PropertyChanged</code> just listens for changes in the <code>Content</code> string property of the ViewModel and <code>ContentChanged</code> is a handler for <code>TextChanged</code> 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 <code>TextGetOptions.None</code> with <code>TextGetOptions.FormatRtf</code> the ViewModel's <code>Content</code> property gets even more messy adding something that looks like empty RTF paragraphs.</p> <p>Here's the Content property definition in the ViewModel so you can ensure that everything's ok with it:</p> <pre><code> /// &lt;summary&gt; /// The &lt;see cref="Content" /&gt; property's name. /// &lt;/summary&gt; public const string ContentPropertyName = "Content"; private string _content; /// &lt;summary&gt; /// Sets and gets the Content property. /// Changes to that property's value raise the PropertyChanged event. /// &lt;/summary&gt; public string Content { get { return _content; } set { if (_content == value) { return; } RaisePropertyChanging(ContentPropertyName); _content = value; RaisePropertyChanged(ContentPropertyName); } } </code></pre> <p>EDIT:</p> <p>Some experiments:</p> <pre><code> 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" </code></pre> <p>EDIT:</p> <p>Another experiment:</p> <p>A simple workaround for <code>TextGetOptions.None</code> is trimming that extra "\r" on output. However with <code>TextGetOptions.FormatRtf</code> things aren't just that simple:</p> <pre><code> 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 </code></pre> <p>I apologize for my English. All corrections concerning it are also welcome :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload