This is a migrated thread and some comments may be shown as answers.

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

1 Answer 172 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Babu
Top achievements
Rank 1
Babu asked on 02 Dec 2014, 03:03 PM
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.

1 Answer, 1 is accepted

Sort by
0
Svetoslav
Telerik team
answered on 05 Dec 2014, 12:27 PM
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.

 
Tags
RichTextBox
Asked by
Babu
Top achievements
Rank 1
Answers by
Svetoslav
Telerik team
Share this question
or