Question

I want to append richTextBox2.Document to richTextBox1, but WPF dont support to joint 2 FlowDocument. Is any way to do this? Thank you very much!

Was it helpful?

Solution

using System.Text.RegularExpressions;
using System.Windows.Markup;

    richTextBox1.Document = Joint_FlowDocument(richTextBox1.Document, richTextBox2.Document);

    //Joint flowDocument_2 to the end of flowDocument_1
    private FlowDocument Joint_FlowDocument(FlowDocument flowDocument_1, FlowDocument flowDocument_2)
    {
        StringWriter wr_f1 = new StringWriter();
        XamlWriter.Save(flowDocument_1, wr_f1);
        string str_f1 = wr_f1.ToString().Replace("</FlowDocument>", "");
        StringWriter wr_f2 = new StringWriter();
        XamlWriter.Save(flowDocument_2, wr_f2);
        string str_f2 = wr_f2.ToString();
        str_f2 = Regex.Replace(str_f2, "<FlowDocument.*?>", "");
        return XamlReader.Parse(str_f1 + str_f2) as FlowDocument;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top