dotnet/maui · error · ArgumentException

The element must be a DependencyObject

Error message

The element must be a DependencyObject

What it means

WindowChrome.GetIsHitTestVisibleInChrome throws ArgumentException when the IInputElement passed in cannot be cast to DependencyObject. The attached property IsHitTestVisibleInChrome is stored on DependencyObject, so a non-DependencyObject input cannot carry the property value. The null check is done first via Verify.IsNotNull.

Source

Thrown at src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/WindowChrome.cs:124

			Verify.IsNotNull(window, "window");
			window.SetValue(WindowChromeProperty, chrome);
		}

		public static readonly DependencyProperty IsHitTestVisibleInChromeProperty = DependencyProperty.RegisterAttached(
			"IsHitTestVisibleInChrome",
			typeof(bool),
			typeof(WindowChrome),
			new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));

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

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

		public static readonly DependencyProperty ResizeGripDirectionProperty = DependencyProperty.RegisterAttached(

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Pass a DependencyObject-derived element (UIElement, FrameworkElement, ContentElement, etc.).
  2. Type-check before calling: if (inputElement is DependencyObject) before Get/Set.
  3. If you hold a UIElement, pass it directly rather than wrapping it.

Example fix

// before
var visible = WindowChrome.GetIsHitTestVisibleInChrome(customInputElement);

// after
if (inputElement is DependencyObject dob)
{
    var visible = WindowChrome.GetIsHitTestVisibleInChrome(dob);
}
else
{
    throw new ArgumentException("Element must be a DependencyObject.", nameof(inputElement));
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Pre-check: element must be a DependencyObject
if (inputElement is not DependencyObject)
{
    throw new ArgumentException("Element must be a DependencyObject.", nameof(inputElement));
}
var visible = WindowChrome.GetIsHitTestVisibleInChrome(inputElement);

Type guard

static bool IsDependencyObject(IInputElement element)
{
    return element is DependencyObject;
}

Prevention

When it happens

Trigger: Calling WindowChrome.GetIsHitTestVisibleInChrome(inputElement) with an IInputElement implementation that is not a DependencyObject — e.g. a custom IInputElement wrapper, a third-party input element, or a mock that does not derive from DependencyObject.

Common situations: Passing a custom IInputElement that wraps a UIElement but is not itself a DependencyObject; test doubles; interop with non-WPF input element implementations; confusion between raw IInputElement and the WPF UIElement hierarchy.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/ca85de373c8daf70. Report an issue: GitHub.