如何将 FlowDocument 添加到 StackPanel?
我创建了以下类,以便有一种简单的方法在 WPF 文档中显示格式化文本。
然而,此解决方案返回一个 FlowDocument,并且我在当前应用程序中集成此 FlowDocument 时遇到了麻烦,在该应用程序中我只是将 TextBlocks 添加到 StackPanels 中,并且WrapPanel 和边框等。
如何将我创建的 FlowDocument 对象添加到现有的 StackPanel、边框和 WrapPanel 中?
替代文本 http://www.deviantsart.com/upload/11jt33i.png
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Controls;
namespace TestFlow23993
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
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}.");
sp.Children.Add(fd); //error
bd.Child = fd; //error
}
}
public class FlowDocumentParser
{
private string markedUpText;
private List<string> parts;
private FlowDocument doc;
public int FontSize { get; set; }
public string FontFamily { get; set; }
public TextAlignment TextAlignment { get; set; }
public FlowDocumentParser(string markedUpText)
{
this.markedUpText = markedUpText;
parts = markedUpText.Split(new char[] { '{', '}' }).ToList();
SetDefaults();
}
void SetDefaults()
{
FontSize = 12;
FontFamily = "Arial";
TextAlignment = TextAlignment.Left;
}
public void BuildFlowDocument()
{
doc = new FlowDocument();
Paragraph paragraph = new Paragraph();
paragraph.TextAlignment = TextAlignment;
doc.Blocks.Add(paragraph);
foreach (var part in parts)
{
//ITALIC
if (part.StartsWith("i:"))
{
string text = part.ChopLeft("i:");
Run run = AddPart(paragraph, text);
run.FontStyle = FontStyles.Italic;
}
//BOLD
else if (part.StartsWith("b:"))
{
string text = part.ChopLeft("b:");
Run run = AddPart(paragraph, text);
run.FontWeight = FontWeights.Bold;
}
//EXAMPLE TEXT
else if (part.StartsWith("ex:"))
{
string text = part.ChopLeft("ex:");
Run run = AddPart(paragraph, text);
run.FontWeight = FontWeights.Bold;
run.Foreground = new SolidColorBrush(Colors.Brown);
}
else
{
Run run = AddPart(paragraph, part);
}
}
}
Run AddPart(Paragraph paragraph, string text)
{
Run run = new Run(text);
paragraph.Inlines.Add(run);
run.FontSize = FontSize;
run.FontFamily = new FontFamily(FontFamily);
return run;
}
public FlowDocument GetBuiltFlowDocument()
{
return doc;
}
public static FlowDocument GetFlowDocument(string markedUpText)
{
FlowDocumentParser fdp = new FlowDocumentParser(markedUpText);
fdp.BuildFlowDocument();
return fdp.GetBuiltFlowDocument();
}
}
public static class StringHelpers
{
public static string ChopLeft(this string line, string removeThis)
{
int removeThisLength = removeThis.Length;
if (line.Length >= removeThisLength)
{
if (line.StartsWith(removeThis))
return line.Substring(removeThisLength, line.Length - removeThisLength);
else
return line;
}
else
return line;
}
}
}
I created the following class in order to have an easy way to display formatted text in a WPF document.
However this solution returns a FlowDocument, and I am having trouble integrating this FlowDocument in my current application in which I was simply adding TextBlocks to StackPanels and WrapPanels and Borders, etc.
How can I add the FlowDocument objects that I create to my existing StackPanels, Borders, and WrapPanels?
alt text http://www.deviantsart.com/upload/11jt33i.png
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Controls;
namespace TestFlow23993
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
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}.");
sp.Children.Add(fd); //error
bd.Child = fd; //error
}
}
public class FlowDocumentParser
{
private string markedUpText;
private List<string> parts;
private FlowDocument doc;
public int FontSize { get; set; }
public string FontFamily { get; set; }
public TextAlignment TextAlignment { get; set; }
public FlowDocumentParser(string markedUpText)
{
this.markedUpText = markedUpText;
parts = markedUpText.Split(new char[] { '{', '}' }).ToList();
SetDefaults();
}
void SetDefaults()
{
FontSize = 12;
FontFamily = "Arial";
TextAlignment = TextAlignment.Left;
}
public void BuildFlowDocument()
{
doc = new FlowDocument();
Paragraph paragraph = new Paragraph();
paragraph.TextAlignment = TextAlignment;
doc.Blocks.Add(paragraph);
foreach (var part in parts)
{
//ITALIC
if (part.StartsWith("i:"))
{
string text = part.ChopLeft("i:");
Run run = AddPart(paragraph, text);
run.FontStyle = FontStyles.Italic;
}
//BOLD
else if (part.StartsWith("b:"))
{
string text = part.ChopLeft("b:");
Run run = AddPart(paragraph, text);
run.FontWeight = FontWeights.Bold;
}
//EXAMPLE TEXT
else if (part.StartsWith("ex:"))
{
string text = part.ChopLeft("ex:");
Run run = AddPart(paragraph, text);
run.FontWeight = FontWeights.Bold;
run.Foreground = new SolidColorBrush(Colors.Brown);
}
else
{
Run run = AddPart(paragraph, part);
}
}
}
Run AddPart(Paragraph paragraph, string text)
{
Run run = new Run(text);
paragraph.Inlines.Add(run);
run.FontSize = FontSize;
run.FontFamily = new FontFamily(FontFamily);
return run;
}
public FlowDocument GetBuiltFlowDocument()
{
return doc;
}
public static FlowDocument GetFlowDocument(string markedUpText)
{
FlowDocumentParser fdp = new FlowDocumentParser(markedUpText);
fdp.BuildFlowDocument();
return fdp.GetBuiltFlowDocument();
}
}
public static class StringHelpers
{
public static string ChopLeft(this string line, string removeThis)
{
int removeThisLength = removeThis.Length;
if (line.Length >= removeThisLength)
{
if (line.StartsWith(removeThis))
return line.Substring(removeThisLength, line.Length - removeThisLength);
else
return line;
}
else
return line;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将 FlowDocument 包装在 FlowDocumentReader 内,则可以将其添加到面板或边框:
If you wrap your FlowDocument inside a FlowDocumentReader then you can add it to the panel or border: