dotnet/wpf · error · ArgumentException
The element must be a DependencyObject
Error message
The element must be a DependencyObject
What it means
WindowChrome.IsHitTestVisibleInChrome is an attached property stored on DependencyObjects, but the getter accepts the broader IInputElement type for convenience. When the passed element implements IInputElement but is not a DependencyObject, the cast fails and ArgumentException is thrown, since there is nowhere to read the attached value.
Solutions
- Pass a WPF element derived from DependencyObject (UIElement, FrameworkElement, ContentElement).
- Add a check: if (element is DependencyObject) before calling.
- If the element is truly not a DependencyObject, the attached property cannot be used — track the flag externally instead.
- Call the getter with the actual Control/element instance rather than an interface wrapper.
Example fix
// before
IInputElement target = GetInputTarget();
bool v = WindowChrome.GetIsHitTestVisibleInChrome(target); // may throw
// after
if (target is DependencyObject dobj)
bool v = WindowChrome.GetIsHitTestVisibleInChrome(dobj); Defensive patterns
Strategy: type-guard
Validate before calling
if (element == null) throw new ArgumentNullException(nameof(element));
if (element is not DependencyObject)
throw new ArgumentException("Element must be a DependencyObject", nameof(element)); Type guard
bool CanUseAttachedChromeProps(IInputElement e) => e is DependencyObject;
Try / catch
try { var v = WindowChrome.GetIsHitTestVisibleInChrome(el); }
catch (ArgumentException) { /* element is not a DependencyObject */ } Prevention
- Type element parameters as DependencyObject instead of IInputElement.
- Check `is DependencyObject` before using WindowChrome attached properties.
- Avoid interop input adapters when reading attached properties.
- Remember all WPF visual elements (UIElement) are DependencyObjects.
When it happens
Trigger: GetIsHitTestVisibleInChrome(myElement) where myElement implements IInputElement (e.g. a custom raw input target or interop adapter) but does not derive from DependencyObject (UIElement/ContentElement).
Common situations: Passing wrapper/adapter objects from interop scenarios; passing non-visual input sources; generic code typed to IInputElement that assumes WPF elements.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ID is not a valid XSD ID.
- array
- Cannot pass multidimensional array to the CopyTo method on…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/10b56f8eed01214e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Shell/WindowChrome.cs:118
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", nameof(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", nameof(inputElement));
}
dobj.SetValue(IsHitTestVisibleInChromeProperty, hitTestVisible);
}
public static readonly DependencyProperty ResizeGripDirectionProperty = DependencyProperty.RegisterAttached(View on GitHub (pinned to 81131a70a4)