views:

195

answers:

1

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

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;
        }
    }
}
+2  A: 

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;
Leom Burke
Edward followed up this question with one about adding multiple flowdocuments to a stackpanel. I have also answered that question but it needs saying here as well. you can use a FlowDocumentScrollViewer which will size to content before available space. using this allows multiple documents on the same form easilly
Leom Burke
yes, here is the other question: http://stackoverflow.com/questions/2288161/how-can-i-add-multiple-flowdocumentreaders-to-a-stackpanel
Edward Tanguay