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
- Pass a DependencyObject-derived element (UIElement, FrameworkElement, ContentElement, etc.).
- Type-check before calling: if (inputElement is DependencyObject) before Get/Set.
- 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
- Pass UIElement/FrameworkElement directly rather than wrapping IInputElement.
- Pattern-match with 'is DependencyObject' before calling attached-property getters.
- Avoid custom IInputElement implementations unless they derive from DependencyObject.
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
- Temporary removed exception message
- '{0}' Transition could not be found!
- Unable to find the required services. Please add all the req
- RootComponent requires a value for its Selector property, bu
- RootComponent requires a value for its ComponentType propert
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/ca85de373c8daf70.
Report an issue: GitHub.