2012-02-22 85 views
1

我有一些正在从数据库中读入的文本。此文本是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)。

我无法弄清的是如何将文本转换为流文档,以便我可以将它与文档查看器相关联(并且我是一个noob)。

任何人都可以帮忙吗?

回答

6

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

+0

这个工作太棒了!谢谢! – 2012-02-22 21:56:47

+0

@TheSheekGeek:很高兴听到这个,欢迎你:) – 2012-02-22 22:00:10

0

让我补充的完整代码:

public static FlowDocument ToFlowDocument(string xmlString) 
{ 
    var result = new FlowDocument(); 

    if (!string.IsNullOrEmpty(xmlString)) 
    { 
     result.Blocks.Add((Section)XamlReader.Parse(xmlString)); 
    } 

    return result;   
}