【问题标题】:How to insert XAML into RichTextBox?如何将 XAML 插入 RichTextBox?
【发布时间】:2010-11-29 18:22:05
【问题描述】:

存储在数据库中的 XAML 文本,如何在通过 XmlReader 读取 XAML 后在 RichTextBox 中显示其文本?

StringReader stringReader = new StringReader(xamlString);
XmlReader xmlReader = XmlReader.Create(stringReader);

rt.Document = ???

------更新------ 这是 xamlString 的内容:

<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Left" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="RightToLeft" NumberSubstitution.CultureSource="User" NumberSubstitution.Substitution="AsCulture" FontFamily="Tahoma" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="13" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0"><Paragraph><Run xml:lang="fa-ir">Hello</Run><Run xml:lang="fa-ir" FontStyle="Italic">Hello1</Run><Run xml:lang="fa-ir" FontWeight="Bold">Testing</Run><Run xml:lang="fa-ir">Testing2</Run></Paragraph></Section>

【问题讨论】:

    标签: c# wpf xaml richtextbox


    【解决方案1】:

    从 RichTextBox 检索 XAML 文本:

    private static string GetRTF(RichTextBox rt)
    {
        TextRange range = new TextRange(rt.Document.ContentStart, rt.Document.ContentEnd);
        MemoryStream stream = new MemoryStream();
        range.Save(stream, DataFormats.Xaml);
        string xamlText = Encoding.UTF8.GetString(stream.ToArray());
        return xamlText;
    }
    

    将 XAML 文本呈现到 RichTextBox 中:

    private static FlowDocument SetRTF(string xamlString)
    {
        StringReader stringReader = new StringReader(xamlString);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        Section sec = XamlReader.Load(xmlReader) as Section;
        FlowDocument doc = new FlowDocument();
        while (sec.Blocks.Count > 0)
            doc.Blocks.Add(sec.Blocks.FirstBlock);
        return doc;
    }
    

    【讨论】:

      【解决方案2】:

      您可以拨打RichTextBox.AppendText(string)

      【讨论】:

      • 谢谢,但我需要在 RichTextBox 中显示格式化文本 (RTF)。问题已更新。
      猜你喜欢
      • 2022-08-15
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多