HandyOrg/HandyControl · error · ArgumentException

The element must be a DependencyObject

Error message

The element must be a DependencyObject

What it means

WindowChrome.GetIsHitTestVisibleInChrome reads the IsHitTestVisibleInChrome attached property, which requires the element to be a DependencyObject so GetValue can be used. The parameter is typed IInputElement (broader), so a non-DependencyObject implementation throws ArgumentException.

Solutions

  1. Ensure the element derives from System.Windows.UIElement or FrameworkElement (both are DependencyObjects)
  2. Cast and check: if (element is DependencyObject) before calling
  3. Prefer the UIElement overload/usage so the type system enforces this at compile time

Example fix

// before
IInputElement el = new CustomInputElement(); // not a DependencyObject
bool v = WindowChrome.GetIsHitTestVisibleInChrome(el);
// after
var el = new Button(); // any UIElement
if (el is DependencyObject)
    bool v = WindowChrome.GetIsHitTestVisibleInChrome(el);
Defensive patterns

Strategy: type-guard

Validate before calling

if (inputElement is not DependencyObject) throw new ArgumentException("Element must derive from DependencyObject (e.g. UIElement)", nameof(inputElement));

Type guard

bool IsChromeCompatible(IInputElement e) => e is DependencyObject;

Try / catch

try { bool v = WindowChrome.GetIsHitTestVisibleInChrome(el); } catch (ArgumentException) { v = false; /* default for non-DependencyObject */ }

Prevention

When it happens

Trigger: Calling WindowChrome.GetIsHitTestVisibleInChrome with an IInputElement that is not a DependencyObject — i.e. a custom class implementing IInputElement directly rather than deriving from UIElement/ContentElement.

Common situations: Custom input elements or mocks implementing IInputElement without inheriting from UIElement; passing test doubles that only implement the interface.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/8e327d70c45487bf. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/Microsoft.Windows.Shell/WindowChrome.cs:62

    }

    [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
    [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
    public static void SetWindowChrome(Window window, WindowChrome chrome)
    {
        Verify.IsNotNull<Window>(window, "window");
        window.SetValue(WindowChrome.WindowChromeProperty, chrome);
    }

    [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
    [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
    public static bool GetIsHitTestVisibleInChrome(IInputElement inputElement)
    {
        Verify.IsNotNull<IInputElement>(inputElement, "inputElement");
        DependencyObject dependencyObject = inputElement as DependencyObject;
        if (dependencyObject == null)
        {
            throw new ArgumentException("The element must be a DependencyObject", "inputElement");
        }
        return (bool) dependencyObject.GetValue(WindowChrome.IsHitTestVisibleInChromeProperty);
    }

    [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
    [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
    public static void SetIsHitTestVisibleInChrome(IInputElement inputElement, bool hitTestVisible)
    {
        Verify.IsNotNull<IInputElement>(inputElement, "inputElement");
        DependencyObject dependencyObject = inputElement as DependencyObject;
        if (dependencyObject == null)
        {
            throw new ArgumentException("The element must be a DependencyObject", "inputElement");
        }
        dependencyObject.SetValue(WindowChrome.IsHitTestVisibleInChromeProperty, hitTestVisible);
    }

    public double CaptionHeight

View on GitHub (pinned to 2c0875ebd6)