Skip to content

fix(adorner): guard against null instance in AdornerElement.OnInstanceChanged (closes #239) - #249

Merged
ghost1372 merged 1 commit into
ghost1372:masterfrom
omid-io:fix/poptip-instance-nullref
Sep 6, 2026
Merged

ghost1372 merged 1 commit into
ghost1372:masterfrom
omid-io:fix/poptip-instance-nullref

Conversation

@omid-io

@omid-io omid-io commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #239 where switching or unbinding a DataTemplate containing a control with an AdornerElement (such as PopTip.Instance) throws a NullReferenceException in AdornerElement.OnInstanceChanged.

Root Cause

In AdornerElement.cs:

private static void OnInstanceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (d is not FrameworkElement target) return;
    var element = (AdornerElement) e.NewValue;
    element.OnInstanceChanged(target);
}

When e.NewValue is null (e.g. when a DataTemplate is unloaded, cleared, or rebound), (AdornerElement) e.NewValue evaluates to null, causing element.OnInstanceChanged(target) to throw a NullReferenceException.

Solution

Safely pattern-match and guard e.NewValue:

private static void OnInstanceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (d is not FrameworkElement target) return;
    if (e.NewValue is AdornerElement element)
    {
        element.OnInstanceChanged(target);
    }
}

Verification

  • Clean build of HandyControl_Net_GE45.csproj targeting net8.0-windows with 0 warnings and 0 errors.

@ghost1372
ghost1372 merged commit 8c61a44 into ghost1372:master Sep 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants