如何将 FlowDocument 添加到 StackPanel?

发布于 2024-08-22 04:52:31 字数 4523 浏览 10 评论 0原文

我创建了以下,以便有一种简单的方法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-08-29 04:52:31

如果将 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文