![](/img/trans.png)
[英]How can I add multiple FlowDocumentReaders to a StackPanel?
[英]How can I add a FlowDocument to a StackPanel?
我創建了以下類 ,以便有一種簡單的方法在WPF文檔中顯示格式化文本。
但是這個解決方案返回一個FlowDocument ,我無法在我當前的應用程序中集成這個FlowDocument,我只是將TextBlocks添加到StackPanels和WrapPanels和Borders等。
如何將我創建的FlowDocument對象添加到現有的StackPanels,Borders和WrapPanels?
替代文字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;
}
}
}
如果將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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.