inline does not belong to this document

3 posts, 0 answers
  1. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 29 Dec 2012 Link to this post


    Is there any reason when executing either of the following two commands I should get the following error?

    Command Executing:
    //Loop Through Document Sections
    foreach (Section sectionLayoutBox in radRichTextBox.Document.Children)
    {
        //Loop Through Section Paragraphs
        foreach (Paragraph paragraph in sectionLayoutBox.EnumerateChildrenOfType<Paragraph>().ToList<Paragraph>())
        {
            //Remove Paragraph From Document
            //paragraph.Parent.Children.Remove(paragraph);
            sectionLayoutBox.Children.Remove(paragraph);
        }
    }


    Error Message: inline does not belong to this document.

    I know my initial post is a little vague, I can elaborate in more detail if you require.

    Thanks,

    Rob
  2. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 29 Dec 2012 Link to this post

    Here's more code if it helps:

    //Room to change found flag
    bool roomToChangeFound = false;
     
    //List of Document Fragments (paragraphs after room title)
    List<DocumentFragment> paragraphsAfterRoom = new List<DocumentFragment>();
     
     
    //Loop Through Document Sections
    foreach (Section sectionLayoutBox in radRichTextBox.Document.Children)
    {
        //Loop Through Section Paragraphs
        foreach (Paragraph paragraph in sectionLayoutBox.EnumerateChildrenOfType<Paragraph>().ToList<Paragraph>())
        {
            //Loop Through Paragraph AnnotationRanges
            foreach (AnnotationRangeMarkerBase annotationMarker in paragraph.EnumerateChildrenOfType<AnnotationRangeMarkerBase>().ToList<AnnotationRangeMarkerBase>())
            {
                //Check if Room Annotation has been found in Paragraph
                if (annotationMarker is RoomGroupRangeStart)
                {
                    if (annotationMarker == oldRoom)
                    {
                        //Flag Room To Remove Has Been Found
                        roomToChangeFound = true;
                        break;
                    }
                    else
                    {
                        roomToChangeFound = false;
                        break;
                    }
                }
            }
     
            //Create List of Paragraphs To Move Following Room Title
            if (roomToChangeFound == true)
            {
                //Clear Document Selection
                radRichTextBox.Document.Selection.Clear();
     
                //Get Paragraph start & end positions
                DocumentPosition startPosition = new DocumentPosition(radRichTextBox.Document.CaretPosition);
                startPosition.MoveToInline(paragraph.Inlines.First);
     
                DocumentPosition endPosition = new DocumentPosition(startPosition);
                endPosition.MoveToLastPositionInParagraph();
     
                //Select Paragraph
                radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
                radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
     
                //Store Paragraph In List<>
                DocumentFragment fragment = radRichTextBox.Document.Selection.CopySelectedDocumentElements();
                paragraphsAfterRoom.Add(fragment);
     
                //Clear Selection
                radRichTextBox.Document.Selection.Clear();
     
                //Remove Paragraph From Document
                //paragraph.Parent.Children.Remove(paragraph);
                sectionLayoutBox.Children.Remove(paragraph);
            }
            else if (roomToChangeFound == false && paragraphsAfterRoom.Count > 0)
            {
                break;
            }
        }
    }
  3. UI for WPF is Visual Studio 2017 Ready
  4. Petya
    Admin
    Petya avatar
    975 posts

    Posted 02 Jan 2013 Link to this post

    Hi Rob,

    A possible reason for such a behavior is the method you choose for deleting the desired paragraphs. Overall, you should not add or remove document elements directly into the structure of a measured document (document shown in the editor), as that may cause document invalidation and lead to errors such as the one observed.

    Instead, you should take advantage of the Delete method of RadRichTextBox which works over the current selection. Respectively, the InsertFragment method can be used for inserting document elements at the caret location. In your case the deletion can happen prior to clearing the selection.

    If this does not help we would appreciate if you open a support ticket and send us a project we can use for testing.
     
    Kind regards,
    Petya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top