title: Bug in System.Windows.Control.RichTextBox
body: <p>I am creating a control in WPF that shows units using a <code>System.Windows.Control.RichTextBox</code>.</p>
  
  <p>The problem is the RichTextBox control shows a plain text instead of a formatted text.
  I guess the RichTextBox control has a bug and I don't know how to do it, because it works depending on the computer.</p>
  
  <p>The XAML code is,</p>
  
  <pre><code>&lt;RichTextBox x:FieldModifier="private"
               x:Name="TxtItem1"
               IsReadOnly="True"
               IsHitTestVisible="False"
               HorizontalContentAlignment="Center"
               VerticalContentAlignment="Center" /&gt;
  </code></pre>
  
  <p>And part of the code behind is:</p>
  
  <pre><code>private static void UpdateDocument(RichTextBox control, DependencyPropertyChangedEventArgs e)
  {
      string content = e.NewValue as string;
      control.Document = content.Html1ToFlowDocument();
  }
  </code></pre>
  
  <p>The function Html1ToFlowDocument converts a string into a FlowDocument. The following image is in a computer that the code goes fine (Windows 7 64 bits):</p>
  
  <p><img src="https://i.stack.imgur.com/Hg7H8.png" alt="GOES"></p>
  
  <p>And the next one does not work (Windows 7 64 bits):</p>
  
  <p><img src="https://i.stack.imgur.com/uqIJm.png" alt="WRONG"></p>
  
  <p>Another approach was using a RTF text but I have the problem.</p>
  
  <p>The code of the function Html1ToFlowDocument,</p>
  
  <pre><code>public static class Html1ToDocument
  {
      public static FlowDocument Html1ToFlowDocument(this string text)
      {
          var mcFlowDoc = new FlowDocument();
  
          XmlDocument doc = new XmlDocument();
          doc.LoadXml(string.Format("&lt;P&gt;{0}&lt;/P&gt;", text));
  
          XmlElement root = doc.GetElementsByTagName("P")[0] as XmlElement;
  
          IEnumerable&lt;Inline&gt; children;
          try
          {
              children = ParseChildren(root);
          }
          catch (Exception ex)
          {
              throw new FormatException("Unsupported text.", ex);
          }
  
          var paragraph = new Paragraph();
          paragraph.Inlines.AddRange(children);
  
          mcFlowDoc.Blocks.Add(paragraph);
          return mcFlowDoc;
      }
  
      private static IEnumerable&lt;Inline&gt; ParseChildren(XmlElement root)
      {
          Span sitem;
          List&lt;Inline&gt; children;
  
          foreach (XmlNode element in root.ChildNodes)
          {
              Inline item = null;
              if (element is XmlElement)
              {
                  XmlElement xelement = (XmlElement)element;
                  switch (xelement.Name.ToUpper())
                  {
                      case "SUB":
                          children = ParseChildren(xelement).ToList();
                          if (children.Count == 1 &amp;&amp; children.First() is Run)
                          {
                              item = children.First();
                              item.Typography.Variants = FontVariants.Subscript;
                          }
                          else
                          {
                              sitem = new Span();
                              sitem.Typography.Variants = FontVariants.Subscript;
                              sitem.Inlines.AddRange(children);
                              item = sitem;
                          }
  
                          break;
                      case "SUPER":
                          children = ParseChildren(xelement).ToList();
                          if (children.Count == 1 &amp;&amp; children.First() is Run)
                          {
                              item = children.First();
                              item.Typography.Variants = FontVariants.Superscript;
                          }
                          else
                          {
                              sitem = new Span();
                              sitem.Typography.Variants = FontVariants.Superscript;
                              sitem.Inlines.AddRange(children);
                              item = sitem;
                          }
                          break;
                  }
  
                  yield return item;
              }
              else if (element is XmlText)
              {
                  item = new Run(element.InnerText);
                  yield return item;
              }
          }
      }
  }
  </code></pre>