dotnet / dotnet/wpf

mouse over image inside DocumentPageView inside FixedPage throws ArgumentNullException

Open
#2,969 2 comments 3 reactions 0 assignees View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: 3.1.201
* Windows version: 10.0.18362
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
* Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? no

**Problem description:**
Hosting a DocumentPageView containing a BlockUIContainer(Image) inside a FixedDocument and presenting it in a DocumentViewer, when moving the mouse over the image, an ArgumentNullException is thrown at System.Windows.LogicalTreeHelper.GetChildren(DependencyObject current)

**Actual behavior:**
System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null.
Parameter name: current
Source=PresentationFramework
StackTrace:
at System.Windows.LogicalTreeHelper.GetChildren(DependencyObject current)
at System.Windows.Documents.FixedPage._CreateChildIndex(DependencyObject e)
at System.Windows.Documents.FixedTextView.GetTextPositionFromPoint(Point point, Boolean snapToText)
at MS.Internal.Documents.TextViewBase.System.Windows.Documents.ITextView.GetTextPositionFromPoint(Point point, Boolean snapToText)
at MS.Internal.Documents.DocumentPageTextView.GetTextPositionFromPoint(Point point, Boolean snapToText)
at MS.Internal.Documents.MultiPageTextView.GetTextPositionFromPoint(Point point, Boolean snapToText)
at MS.Internal.Documents.TextViewBase.System.Windows.Documents.ITextView.GetTextPositionFromPoint(Point point, Boolean snapToText)
at System.Windows.Documents.TextEditorMouse.IsPointWithinInteractiveArea(TextEditor textEditor, Point point)
at System.Windows.Documents.TextEditorMouse.OnQueryCursor(Object sender, QueryCursorEventArgs e)
at System.Windows.Input.QueryCursorEventArgs.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.MouseDevice.UpdateCursorPrivate()
at System.Windows.Input.MouseDevice.PostProcessInput(Object sender, ProcessInputEventArgs e)
at System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs)
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 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at WpfApp3.App.Main()

This exception was originally thrown at this call stack:
System.Windows.LogicalTreeHelper.GetChildren(System.Windows.DependencyObject)
System.Windows.Documents.FixedPage._CreateChildIndex(System.Windows.DependencyObject)
System.Windows.Documents.FixedTextView.GetTextPositionFromPoint(System.Windows.Point, bool)
MS.Internal.Documents.TextViewBase.System.Windows.Documents.ITextView.GetTextPositionFromPoint(System.Windows.Point, bool)
MS.Internal.Documents.DocumentPageTextView.GetTextPositionFromPoint(System.Windows.Point, bool)
MS.Internal.Documents.MultiPageTextView.GetTextPositionFromPoint(System.Windows.Point, bool)
MS.Internal.Documents.TextViewBase.System.Windows.Documents.ITextView.GetTextPositionFromPoint(System.Windows.Point, bool)
System.Windows.Documents.TextEditorMouse.IsPointWithinInteractiveArea(System.Windows.Documents.TextEditor, System.Windows.Point)
System.Windows.Documents.TextEditorMouse.OnQueryCursor(object, System.Windows.Input.QueryCursorEventArgs)
System.Windows.Input.QueryCursorEventArgs.InvokeEventHandler(System.Delegate, object)
...
[Call Stack Truncated]

**Expected behavior:**
No exception (OR more descriptive exception, in case the method here is wrong to begin with?)

**Minimal repro:**
* Create a new WpfApplication
* add any image.png as a resource in the project
* Add a DocumentViewer to mainWindow.xaml:
``
* Add some code behind to create a FixedDocument:
```csharp
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

namespace WpfApp2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

public override void OnApplyTemplate()
{
FixedDocument doc = CreateFixedDocument();
Viewer.Document = doc;
}

private FixedDocument CreateFixedDocument()
{
var flowDoc = CreateFlowDocument();
DocumentPaginator paginator = ((IDocumentPaginatorSource) flowDoc).DocumentPaginator;
paginator.ComputePageCount();

// converts the flow document to a fixed document, using a DocumentPageView for each page (only one page)
FixedDocument result = new FixedDocument();
IAddChild fp = new FixedPage();
fp.AddChild(new DocumentPageView
{
DocumentPaginator = paginator,
PageNumber = 0,
});
result.Pages.Add(new PageContent()
{
Child = (FixedPage) fp,
});
return result;
}

private FlowDocument CreateFlowDocument()
{
// Creates a flow document with an image
FlowDocument flowDoc = new FlowDocument();
Image image = new Image()
{
Source = new BitmapImage(
new Uri($"pack://application:,,,/WpfApp2;component/Resources/image.png"))
};
flowDoc.Blocks.Add(new BlockUIContainer(image));
return flowDoc;
}
}
}
```
* Start the application and move the mouse over the image.

I have tested showing the DocumentPageView by itself and it works. It only breaks inside the FixedDocument page.
I thought first that I should use the IAddChild.AddChild() method since the exception is thrown at GetChildren, but that didn't help.
If this is an issue caused by my method here then it would be good to get a more descriptive error.

I can go around the issue by using a Border with `Background = new ImageBrush(...)` instead of an image. That seems to work fine.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.