This project has moved. For the latest updates, please go here.

RichTextBox binding and emptying the content.

Feb 3, 2011 at 6:53 PM

I don't know what your thoughts are on this, but when binding the Text property of RTB to a property which has it's value switched to "" or null or anything that matches string.IsNullorWhitespace, we see the formatting exceptions.  It would seem appropriate to me for the formatter to set the value then to the nearest equivalent of an empty string.  For instance:

        public void SetText(FlowDocument document, string text)
        {
          if(string.IsNullOrWhiteSpace(text))
            text = @"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch }\li0\ri0\sa0\sb0\fi0\ql\par}}}";

            TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);
            using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
            {
                tr.Load(ms, DataFormats.Rtf);
            }
        }

I'm not sure the best way to deal with the apparent formatting injected into that empty constant, but it seems a better answer than the Converter suggested in another post.
Feb 4, 2011 at 2:08 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Feb 11, 2011 at 1:49 PM

This works just fine in Changeset 68280.  Thanks a ton!