【问题标题】:Using a string to create a flow document使用字符串创建流文档
【发布时间】:2012-02-22 20:52:56
【问题描述】:

我有一些正在从数据库中读取的文本。此文本是 xaml 部分对象的字符串表示形式。此文本将根据组合框中的选择而有所不同。以下是一些文本的示例:

<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Left" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="User" NumberSubstitution.Substitution="AsCulture" FontFamily="Verdana" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="11" 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>Hello World      </Run></Paragraph></Section>

我需要获取此文本并将其以正确格式显示在应用程序的控件中。我相信,为了做到这一点,我需要一个FlowDocumentViewer(我正在使用FlowDocumentScrollViewer)。

我不知道如何将文本放入流文档中,以便我可以将它与文档查看器关联(绑定)(我对此很陌生)。

谁能帮忙?

【问题讨论】:

    标签: c# wpf xaml flowdocument flowdocumentscrollviewer


    【解决方案1】:

    您可以使用XamlReader.Parse 从字符串中创建Section,然后您可以将其添加到文档中。

    【讨论】:

      【解决方案2】:

      让我添加完整的代码:

      public static FlowDocument ToFlowDocument(string xmlString)
      {
          var result = new FlowDocument();
      
          if (!string.IsNullOrEmpty(xmlString))
          {
              result.Blocks.Add((Section)XamlReader.Parse(xmlString));
          }
      
          return result;        
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-24
        • 2010-12-09
        • 1970-01-01
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-20
        相关资源
        最近更新 更多