Skip to content

TitleBar - NRE when visibility is Collapsed #1670

@luca-domenichini

Description

@luca-domenichini

Describe the bug

When TitleBar is set to a Collapsed visilibity, the Window it resides in throws NRE continuously when hovering with mouse:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Wpf.Ui.Controls.TitleBar.HwndSourceHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) in C:\dev\wpfui-mimmo\src\Wpf.Ui\Controls\TitleBar\TitleBar.cs:line 679
   at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(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)

In this execution path we are trying to handle the hovering on buttons: if buttons are not yet built, we can safely skip this check.

I checked this issue too #962 but seems not related to this situation.

To Reproduce

  1. Open WPF-UI solution, Wpf.Ui.Gallery project.
  2. Edit Wpf.Ui.Gallery.Views.Windows.MainWindow by adding Visibility="Collapsed" to TitleBar, as in following screenshot:
Image
  1. Set breakpoint at TitleBar line ~678
  2. Run Gallery program and move mouse inside window (bug does not happen when mouse is not hovering Window)
  3. Breakpoint hits with NULL values inside the _buttons array:
Image

The problem is that _buttons array is filled with buttons in OnApplyTemplate() method, that is not called if TitleBar is collapsed.

Expected behavior

The expected behavior is to simply fallback to no-op on buttons hovering check.

Screenshots

No response

OS version

Win11

.NET version

.NET 10

WPF-UI NuGet version

4.2.0

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions