WPF RadRichTextBox - Insert AnnotationRanges using InsertFragment throws exception when Track Changes is ON

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

    Posted 15 Dec 2014 Link to this post

    In WPF RadRichTextBox, we try to Insert AnnotationRanges using InsertFragment method. But, it throws exception in some scenarios when Track Changes is ON. Please find sample code below.

    try
                {
                    //Initialize
                    RadDocument associatedDocument = associatedRichTextBox.Document;
                    DocumentSelection selection = associatedDocument.Selection;
                    DocumentPosition documentPosition = associatedDocument.CaretPosition;

                    string selectedText = GetSelectedText(associatedDocument);

                    // If text is not selected 
                    if (String.IsNullOrEmpty(selectedText) && String.IsNullOrWhiteSpace(selectedText))
                    {
                        MessageBox.Show("Please select a text", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        RadDocument tempDocument = associatedDocument.Selection.CreateDocumentFromSelection();

                        IEnumerable<FormulaRangeStart> formulaMarkers = tempDocument.GetAnnotationMarkersOfType<FormulaRangeStart>();

                        bool bformulaRangeExistsInSelection = false;
                        foreach (FormulaRangeStart frmla in formulaMarkers)
                        {
                            bformulaRangeExistsInSelection = true;
                            break;
                        }

                        if (bformulaRangeExistsInSelection)
                        {
                            MessageBox.Show("Selection already contains a formula range", "Formula Information", MessageBoxButton.OK, MessageBoxImage.Information);
                            return;
                        }

                        FormulaRangeStart formulaRangeStart = new FormulaRangeStart();
                        FormulaRangeEnd formulaRangeEnd = new FormulaRangeEnd();
                        formulaRangeEnd.PairWithStart(formulaRangeStart);

                        tempDocument.CaretPosition.MoveToFirstPositionInDocument();
                        tempDocument.InsertInline(formulaRangeStart);

                        tempDocument.CaretPosition.MoveToLastPositionInDocument();
                        tempDocument.InsertInline(formulaRangeEnd);

                        tempDocument.Selection.SelectAll();

                        IEnumerable<Paragraph> paragraphs = tempDocument.Selection.GetSelectedParagraphs();

                        foreach (Paragraph paragraph in paragraphs)
                        {
                            foreach (Telerik.Windows.Documents.Model.Inline inline in paragraph.Inlines)
                            {
                                if (inline is Span)
                                {
                                    ((Span)(inline)).HighlightColor = Colors.DarkGray;
                                }
                            }
                        }

                        tempDocument.CaretPosition.MoveToLastPositionInDocument();

                        Span spanResetBgColor = new Span(FormattingSymbolLayoutBox.SPACE_SYMBOL_VISUAL);
                        spanResetBgColor.HighlightColor = Colors.White;
                        tempDocument.InsertInline(spanResetBgColor);

                        tempDocument.Selection.SelectAll();

                        associatedRichTextBox.IsTrackChangesEnabled = true;
                        associatedRichTextBox.TrackChangesOptions.TrackFormatting = true;

                        associatedRichTextBox.InsertFragment(tempDocument.Selection.CopySelectedDocumentElements(true));
                        associatedRichTextBox.Insert(FormattingSymbolLayoutBox.ENTER);
                        associatedRichTextBox.Document.Selection.Clear();
                        associatedRichTextBox.UpdateLayout();
                        associatedRichTextBox.UpdateEditorLayout();
                        associatedRichTextBox.Focus();
                        if (associatedRichTextBox.ActiveEditorPresenter != null)
                        {
                            associatedRichTextBox.ActiveEditorPresenter.RecreateUI();
                        }
                    }
  2. Todor
    Admin
    Todor avatar
    168 posts

    Posted 18 Dec 2014 Link to this post

    Hi Babu,

    I've tried to reproduce the issue you described, but to no avail. I've tested the scenario with ReadOnlyRanges instead of "FormulaRanges". It will be great if you can isolate the issue in a sample demo project and send it to us. In this way we can be much more effective in our try to find what causes the issue. It will be also of our great help if you share with us what is the exception, are there any specific circumstances when it occurs as you mentioned the exception is thrown in "some scenarios". Which are these scenarios and can you provide steps to reproduce?

    I'm looking forward to your reply.

    Regards,
    Todor
    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