我如何添加一个的FlowDocument到StackPanel的? [英] How can I add a FlowDocument to a StackPanel?

查看:187
本文介绍了我如何添加一个的FlowDocument到StackPanel的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了下面的为了有一个简单的方法显示的格式文本的WPF文档中



然而,这种解决方案返回的FlowDocument ,然后我有麻烦的整合此FlowDocument的在我目前的应用程序中,我只需添加< STRONG>的TextBlocks 以StackPanels和WrapPanels和边框等。



我怎么能说我创建的FlowDocument对象添加到现有StackPanels,边框和WrapPanels?



 使用System.Collections.Generic; 
使用System.Linq的;使用System.Windows
;使用System.Windows.Documents
;使用System.Windows.Media
;使用System.Windows.Controls的
;

命名空间TestFlow23993
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent() ;

StackPanel的SP =新的StackPanel();
边境BD =新的边界();
的FlowDocument FD = FlowDocumentParser.GetFlowDocument(你想{I:总是}
使用这个库所以它是既{B:容易}和{A:直截了当}至
格式。文件下面是一些例子{b:驼峰}字符串:
{EX:名字},{EX:名字},{如:标题}和{例如:allowUserToFilterRows})。

sp.Children.Add(FD); //错误
bd.Child = FD; //错误


}
}

公共类FlowDocumentParser
{
私人字符串markedUpText;
私有列表<串GT;部分;
私人的FlowDocument文档;

公众诠释字号{搞定;组; }
公共字符串的FontFamily {搞定;组; }
公共TextAlignment TextAlignment {搞定;组; }

公共FlowDocumentParser(字符串markedUpText)
{
this.markedUpText = markedUpText;
部分= markedUpText.Split(新的char [] {'{','}'})了ToList()。
使用setdefaults();
}

无效使用setdefaults()
{
字号= 12;
的FontFamily =宋体;
TextAlignment = TextAlignment.Left;
}

公共无效BuildFlowDocument()
{$ B $(DOC)B =新的FlowDocument();
逐段=新的第();
paragraph.TextAlignment = TextAlignment;
doc.Blocks.Add(段落);

的foreach(在部分VAR部分)
{
// ITALIC
如果(part.StartsWith(I))
{
字符串文本= part.ChopLeft(I);
RUN RUN = AddPart(段落,文字);
run.FontStyle = FontStyles.Italic;
}
// BOLD
,否则如果(part.StartsWith(B))
{
字符串文本= part.ChopLeft(B) ;
RUN RUN = AddPart(段落,文字);
run.FontWeight = FontWeights.Bold;
}
// EXAMPLE TEXT
,否则如果(part.StartsWith(EX))
{
字符串文本= part.ChopLeft(EX );
RUN RUN = AddPart(段落,文字);
run.FontWeight = FontWeights.Bold;
run.Foreground =新的SolidColorBrush(Colors.Brown);
}
,否则
{
RUN RUN = AddPart(段部分);
}
}
}

运行AddPart(逐段字符串文本)
{
RUN RUN =新的运行(文本);
paragraph.Inlines.Add(运行);
run.FontSize =字号;
run.FontFamily =新的FontFamily(的FontFamily);
返回运行;
}

公众的FlowDocument GetBuiltFlowDocument()
{
返回文档;
}

公共静态的FlowDocument GetFlowDocument(字符串markedUpText)
{
FlowDocumentParser FDP =新FlowDocumentParser(markedUpText);
fdp.BuildFlowDocument();
返回fdp.GetBuiltFlowDocument();
}
}

公共静态类StringHelpers
{
公共静态字符串ChopLeft(这串线,串removeThis)
{
INT removeThisLength = removeThis.Length;
如果(line.Length> = removeThisLength)
{
如果(line.StartsWith(removeThis))
返回line.Substring(removeThisLength,line.Length - removeThisLength);
,否则
回线;
}
,否则
回线;
}
}
}


解决方案

如果你换一个FlowDocumentReader里你的FlowDocument,那么你可以把它添加到面板或边框:

 的StackPanel SP =新的StackPanel(); 
边境BD =新的边界();
的FlowDocument FD = FlowDocumentParser.GetFlowDocument(你想{I:总是}使用这个库,它既是{B:容易}和{A:直截了当}。以格式的文档下面是一些例子{乙:驼峰}字符串:{EX:名字},{EX:名字},{如:标题}和{例如:allowUserToFilterRows})。
FlowDocumentReader FDR =新FlowDocumentReader();
fdr.Document = FD;

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


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?

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;
        }
    }
}

解决方案

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;

这篇关于我如何添加一个的FlowDocument到StackPanel的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆