I am creating a control in WPF that shows units using System.Windows.Control.RichTextBox.
The problem is that the RichTextBox control displays plain text instead of rich text. I think the RichTextBox control has an error, and I donβt know how to do it, because it works depending on the computer.
XAML Code,
<RichTextBox x:FieldModifier="private"
x:Name="TxtItem1"
IsReadOnly="True"
IsHitTestVisible="False"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center" />
And the piece of code behind:
private static void UpdateDocument(RichTextBox control, DependencyPropertyChangedEventArgs e)
{
string content = e.NewValue as string;
control.Document = content.Html1ToFlowDocument();
}
The Html1ToFlowDocument function converts a string to a FlowDocument. The following image is on a computer in which the code works fine (Windows 7 64 bit):

And the following does not work (Windows 7 64 bit):

Another approach was to use RTF text, but I have a problem.
Html1ToFlowDocument function code,
public static class Html1ToDocument
{
public static FlowDocument Html1ToFlowDocument(this string text)
{
var mcFlowDoc = new FlowDocument();
XmlDocument doc = new XmlDocument();
doc.LoadXml(string.Format("<P>{0}</P>", text));
XmlElement root = doc.GetElementsByTagName("P")[0] as XmlElement;
IEnumerable<Inline> 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<Inline> ParseChildren(XmlElement root)
{
Span sitem;
List<Inline> 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 && 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 && 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;
}
}
}
}