C# 如何将FlowDocument添加到StackPanel?

C# 如何将FlowDocument添加到StackPanel?,c#,wpf,xaml,flowdocument,stackpanel,C#,Wpf,Xaml,Flowdocument,Stackpanel,我创建了以下类,以便在WPF文档中显示格式化的文本 但是,此解决方案返回一个流程文档,我在当前的应用程序中集成该流程文档时遇到了问题,在该应用程序中,我只是简单地将文本块添加到StackPanel、Wrappanel和Borders等中 如何将创建的FlowDocument对象添加到现有的StackPanel、Borders和Wrappanel中? 使用System.Collections.Generic; 使用System.Linq; 使用System.Windows; 使用System.W

我创建了以下,以便在WPF文档中显示格式化的文本

但是,此解决方案返回一个流程文档,我在当前的应用程序中集成该流程文档时遇到了问题,在该应用程序中,我只是简单地将文本块添加到StackPanel、Wrappanel和Borders等中

如何将创建的FlowDocument对象添加到现有的StackPanel、Borders和Wrappanel中?

使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Documents;
使用System.Windows.Media;
使用System.Windows.Controls;
命名空间TestFlow23993
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
StackPanel sp=新的StackPanel();
边框bd=新边框();
FlowDocument fd=FlowDocumentParser.GetFlowDocument(“您想{i:always}
使用此库,使{b:easy}和{b:straight forward}都可以
格式化文档。以下是一些{b:camel case}字符串示例:
{ex:firstName}、{ex:lastName}、{ex:title}和{ex:allowUserToFilterRows}”);
sp.Children.Add(fd);//错误
bd.Child=fd;//错误
}
}
公共类流文档解析器
{
私有字符串markedUpText;
私人零件清单;
私人文件;
公共int FontSize{get;set;}
公共字符串{get;set;}
公共文本对齐文本对齐{get;set;}
公共FlowDocumentParser(字符串markedUpText)
{
this.markedUpText=markedUpText;
parts=markedUpText.Split(新字符[]{{{'',}}).ToList();
SetDefaults();
}
void SetDefaults()
{
FontSize=12;
FontFamily=“Arial”;
TextAlignment=TextAlignment.Left;
}
public void BuildFlowDocument()
{
doc=新的流程文档();
段落=新段落();
段落.TextAlignment=TextAlignment;
文件块添加(段落);
foreach(var零件中的零件)
{
//斜体
如果(第1部分开始使用(“i:”)
{
字符串文本=part.ChopLeft(“i:”);
运行=添加部分(段落,文本);
run.FontStyle=FontStyles.Italic;
}
//大胆的
else if(部件启动时使用(“b:”)
{
字符串文本=part.ChopLeft(“b:”);
运行=添加部分(段落,文本);
run.fontwweight=fontwweights.Bold;
}
//示例文本
else if(部件启动方式(“ex:”)
{
字符串文本=part.ChopLeft(“ex:”);
运行=添加部分(段落,文本);
run.fontwweight=fontwweights.Bold;
run.Foreground=新的SolidColorBrush(Colors.Brown);
}
其他的
{
运行=添加部分(段落,部分);
}
}
}
运行AddPart(段落、字符串文本)
{
运行=新运行(文本);
段落.内联线.添加(运行);
run.FontSize=FontSize;
run.FontFamily=新FontFamily(FontFamily);
回程;
}
公共流文档GetBuiltFlowDocument()
{
退货单;
}
公共静态FlowDocument GetFlowDocument(字符串markedUpText)
{
FlowDocumentParser fdp=新的FlowDocumentParser(markedUpText);
fdp.BuildFlowDocument();
返回fdp.GetBuiltFlowDocument();
}
}
公共静态类StringHelper
{
公共静态字符串左移(此字符串行,字符串移除此)
{
int removeThisLength=removeThis.Length;
如果(line.Length>=删除此长度)
{
if(线路起始(移除此))
返回line.Substring(removeThisLength,line.Length-removeThisLength);
其他的
回流线;
}
其他的
回流线;
}
}
}

如果将FlowDocument包装在FlowDocumentReader中,则可以将其添加到面板或边框中:

StackPanel sp = new StackPanel();
Border bd = new Border();
FlowDocument fd = FlowDocumentParser.GetFlowDocument("You want to {i:always} use this library so it is both {b:easy} and {b:straight-forward} to format documents. Here are some examples of {b:camel-case} strings: {ex:firstName}, {ex:lastName}, {ex:title}, and {ex:allowUserToFilterRows}.");
FlowDocumentReader fdr = new FlowDocumentReader();
fdr.Document = fd;

sp.Children.Add(fdr);
bd.Child = fdr;

Edward接着问了一个关于向stackpanel添加多个flowdocuments的问题。我也回答了这个问题,但这里也需要说明。您可以使用FlowDocumentScrollViewer,它将根据可用空间之前的内容调整大小。使用此选项可以在同一表单上创建多个文档easillyyes,下面是另一个问题:
StackPanel sp = new StackPanel();
Border bd = new Border();
FlowDocument fd = FlowDocumentParser.GetFlowDocument("You want to {i:always} use this library so it is both {b:easy} and {b:straight-forward} to format documents. Here are some examples of {b:camel-case} strings: {ex:firstName}, {ex:lastName}, {ex:title}, and {ex:allowUserToFilterRows}.");
FlowDocumentReader fdr = new FlowDocumentReader();
fdr.Document = fd;

sp.Children.Add(fdr);
bd.Child = fdr;