我有一些正在从数据库中读取的文本。此文本是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
)。
我无法弄清楚的是如何将文本转换为流文档,以便我可以将它与文档查看器关联(绑定它)(我就是这样的菜鸟)。
有人可以帮忙吗?
答案 0 :(得分:6)
您可以使用XamlReader.Parse
从字符串中创建Section
,然后您可以将其添加到文档中。
答案 1 :(得分:1)
让我添加完整的代码:
public static FlowDocument ToFlowDocument(string xmlString)
{
var result = new FlowDocument();
if (!string.IsNullOrEmpty(xmlString))
{
result.Blocks.Add((Section)XamlReader.Parse(xmlString));
}
return result;
}