WPF RichTextBox - InsertFragment(DocumentFragment) for multiple lines selection - Not Working

2 posts, 0 answers
  1. Babu
    Babu avatar
    11 posts
    Member since:
    Feb 2014

    Posted 02 Dec 2014 Link to this post

    When I select a single line(See screenshot 1) and convert into an annotation range using InsertFragment, it is getting inserted.  (See screenshot 2).

    But, when I select multiple lines(See screenshot 3), InsertFragment is not working.

    Code Sample:

                RadDocument associatedDocument = associatedRichTextBox.Document;
                DocumentSelection selection = associatedDocument.Selection;
                DocumentPosition documentPosition = associatedDocument.CaretPosition;

                string selectedText = GetSelectedText(associatedDocument);

                RadDocument tempDocument = new RadDocument();
                Section section = new Section();
                Paragraph paragraph = new Paragraph();

                try
                {
                    //BookmarkRangeStart bookmarkRangeStart = new BookmarkRangeStart();
                    //BookmarkRangeEnd bookmarkRangeEnd = new BookmarkRangeEnd();
                    //bookmarkRangeEnd.PairWithStart(bookmarkRangeStart);

                    //paragraph.Inlines.Add(bookmarkRangeStart);
                    //paragraph.Inlines.Add(bookmarkRangeEnd);

                    HeadingRangeStart headingRangeStart = new HeadingRangeStart();
                    HeadingRangeEnd headingRangeEnd = new HeadingRangeEnd();
                    headingRangeEnd.PairWithStart(headingRangeStart);

                    Span spanSelectedText = new Span(selectedText.TrimEnd());
                    spanSelectedText.FontWeight = FontWeights.Bold;
                    spanSelectedText.ForeColor = Colors.Red;

                    paragraph.Inlines.Add(headingRangeStart);
                    paragraph.Inlines.Add(spanSelectedText);
                    paragraph.Inlines.Add(headingRangeEnd);

                    section.Blocks.Add(paragraph);
                    tempDocument.Sections.Add(section);
                    tempDocument.MeasureAndArrangeInDefaultSize();
                    tempDocument.Selection.SelectAll();

                    associatedRichTextBox.UpdateEditorLayout();
                    associatedRichTextBox.InsertFragment(tempDocument.Selection.CopySelectedDocumentElements(true));
                    associatedRichTextBox.Document.Selection.Clear();

                    associatedRichTextBox.UpdateEditorLayout();
                }

    Please suggest a solution.
  2. Svetoslav
    Admin
    Svetoslav avatar
    55 posts

    Posted 05 Dec 2014 Link to this post

    Hi Babu,

    Thank you for contacting us.

    I am a bit confused about your scenario. Could you please describe exactly what you are trying to achieve? I also notice that you are trying to convert a selected text to a single span which is not a valid scenario in case you have a text on several new lines as you have shown in attached image "3.jpg". If you debug this code 
    Span spanSelectedText = new Span(selectedText.TrimEnd());
    you will notice that the spanSelectedText.Text will look like this "My Word1\r\nMy Word2\r\nMy Word3" which makes the span invalid.

    I hope this information was helpful. Let me know if you need further assistance.

    Regards,
    Svetoslav
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top