问题
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;
}
}
}
回答1:
If you wrap your FlowDocument inside a FlowDocumentReader then you can add it to the panel or border:
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;
来源:https://stackoverflow.com/questions/2287912/how-can-i-add-a-flowdocument-to-a-stackpanel