FlowDocumentScrollViewer crashes when being scrolled

95 views Asked by At

I'm working on a wpf application that has a FlowDocument that is bound to a FlowDocumentScrollViewer. When scrolling the FlowDocumentScrollViewer, I'm getting an InvalidOperationException: 'This TextNavigator' has no scoping text element. and this causes the application to crash.

Exception Details:

System.InvalidOperationException: 'This TextNavigator' has no scoping text element.
at System.Windows.Documents.TextPointer.MoveToElementEdge(ElementEdge edge)
at System.Windows.Documents.TextPointer.System.Windows.Documents.ITextPointer.MoveToElementEdge(ElementEdge edge)
at System.Windows.Documents.TextPointerBase.GetFollowingNonMergeableInlineContentStart(ITextPointer position)
at System.Windows.Documents.TextSelection.get_PropertyPosition()
at System.Windows.Documents.TextSelection.GetCurrentValue(DependencyProperty formattingProperty)
at System.Windows.Documents.TextSelection.GetCaretBrush(TextEditor textEditor)
at System.Windows.Documents.TextSelection.UpdateCaretStateWorker(Object o)
at System.Windows.Documents.TextSelection.UpdateCaretState(CaretScrollMethod caretScrollMethod)
at System.Windows.Documents.TextSelection.System.Windows.Documents.ITextRange.NotifyChanged(Boolean disableScroll, Boolean skipEvents)
at System.Windows.Documents.TextRangeBase.EndChange(ITextRange thisRange, Boolean disableScroll, Boolean skipEvents)
at System.Windows.Documents.TextRange.System.Windows.Documents.ITextRange.EndChange(Boolean disableScroll, Boolean skipEvents)
at System.Windows.Documents.TextRange.ChangeBlock.System.IDisposable.Dispose()
at System.Windows.Documents.TextEditorMouse.OnMouseMoveWithFocus(TextEditor This, MouseEventArgs e)
at System.Windows.Documents.TextEditorMouse.OnMouseMove(Object sender, MouseEventArgs e)
at System.Windows.Input.MouseEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 

I am not getting any exception when generating the FlowDocument. I've been searching online but found no information that can help.

1

There are 1 answers

0
Karl Stephen On

I don't have a direct explanation, but I encountered the very same exception today. And in my case, the answer is : Don't select an empty BlockUIContainer along with whatever "better suited" FlowDocument component you have there.

This is my xaml, it's an user control, I create an instance and put it in my window (it's supposed to be like an "home page" inside the first TabItem of a TabControl from where I decide what to do in my "swiss knive" application). Note the BlockUIContainer (to be used to host a note-like UI, to paste any text, and save it to a dump file), I commented out its content :

    <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        
        <FlowDocumentScrollViewer 
            IsToolBarVisible="False" Zoom="60" 
            HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <FlowDocument>
                <Paragraph><Run FontSize="24">Quick start :</Run></Paragraph>
                <List MarkerOffset="16" LineHeight="16">
                    <ListItem><Paragraph>Open application directory in <Hyperlink NavigateUri="" RequestNavigate="ExploreAppDir_RequestNavigate">Explorer</Hyperlink>...</Paragraph></ListItem>
                    <ListItem><Paragraph>Manage <Hyperlink NavigateUri="" RequestNavigate="GalleryManager_RequestNavigate">images gallery</Hyperlink>...</Paragraph></ListItem>
                    <ListItem><Paragraph>Create a model...</Paragraph></ListItem>
                    <ListItem><Paragraph>Launch server...</Paragraph></ListItem>
                </List>
                
                <BlockUIContainer>
                    <!--DockPanel Height="100">
                        <TextBox DockPanel.Dock="Top" />
                        <Grid />
                    </DockPanel-->
                </BlockUIContainer>
                
                <Paragraph>
                    <Bold>What to do next ?</Bold>
                </Paragraph>
            </FlowDocument>
        </FlowDocumentScrollViewer>
        
    </DockPanel>

I commented out the content of the BlockUIContainer, and when I start selecting from the text "What to do next" up to the BlockUIContainer component, I get the exception.

When I uncomment the BlockUIContainer content, ie, now I have that textbox and a placeholder grid, I can select anything (any of the FlowDocument items), go through the BlockUIContainer component (incl. its textbox), and beyond, without exception.

I don't understand the exception at all. Based on the words in the callstack, it appears an empty BlockUIContainer cannot forward content selection bounds when there is no child content, which I find like a little oversight. OP's issue occurs when he scrolls, but does not suggest he had any selection at all, so probably another text bounding issue.