{"record":{"id":"10b56f8eed01214e","repo":"dotnet/wpf","slug":"the-element-must-be-a-dependencyobject","errorCode":null,"errorMessage":"The element must be a DependencyObject","messagePattern":"The element must be a DependencyObject","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Shell/WindowChrome.cs","lineNumber":118,"sourceCode":"            Verify.IsNotNull(window, \"window\");\n            window.SetValue(WindowChromeProperty, chrome);\n        }\n\n        public static readonly DependencyProperty IsHitTestVisibleInChromeProperty = DependencyProperty.RegisterAttached(\n            \"IsHitTestVisibleInChrome\",\n            typeof(bool),\n            typeof(WindowChrome),\n            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));\n\n        [SuppressMessage(\"Microsoft.Design\", \"CA1062:Validate arguments of public methods\", MessageId = \"0\")]\n        [SuppressMessage(\"Microsoft.Design\", \"CA1011:ConsiderPassingBaseTypesAsParameters\")]\n        public static bool GetIsHitTestVisibleInChrome(IInputElement inputElement)\n        {\n            Verify.IsNotNull(inputElement, \"inputElement\");\n            var dobj = inputElement as DependencyObject;\n            if (dobj == null)\n            {\n                throw new ArgumentException(\"The element must be a DependencyObject\", nameof(inputElement));\n            }\n            return (bool)dobj.GetValue(IsHitTestVisibleInChromeProperty);\n        }\n\n        [SuppressMessage(\"Microsoft.Design\", \"CA1062:Validate arguments of public methods\", MessageId = \"0\")]\n        [SuppressMessage(\"Microsoft.Design\", \"CA1011:ConsiderPassingBaseTypesAsParameters\")]\n        public static void SetIsHitTestVisibleInChrome(IInputElement inputElement, bool hitTestVisible)\n        {\n            Verify.IsNotNull(inputElement, \"inputElement\");\n            var dobj = inputElement as DependencyObject;\n            if (dobj == null)\n            {\n                throw new ArgumentException(\"The element must be a DependencyObject\", nameof(inputElement));\n            }\n            dobj.SetValue(IsHitTestVisibleInChromeProperty, hitTestVisible);\n        }\n\n        public static readonly DependencyProperty ResizeGripDirectionProperty = DependencyProperty.RegisterAttached(","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Shell/WindowChrome.cs#L100-L136","documentation":"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.","triggerScenarios":"GetIsHitTestVisibleInChrome(myElement) where myElement implements IInputElement (e.g. a custom raw input target or interop adapter) but does not derive from DependencyObject (UIElement/ContentElement).","commonSituations":"Passing wrapper/adapter objects from interop scenarios; passing non-visual input sources; generic code typed to IInputElement that assumes WPF elements.","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."],"exampleFix":"// before\nIInputElement target = GetInputTarget();\nbool v = WindowChrome.GetIsHitTestVisibleInChrome(target); // may throw\n// after\nif (target is DependencyObject dobj)\n    bool v = WindowChrome.GetIsHitTestVisibleInChrome(dobj);","handlingStrategy":"type-guard","validationCode":"if (element == null) throw new ArgumentNullException(nameof(element));\nif (element is not DependencyObject)\n    throw new ArgumentException(\"Element must be a DependencyObject\", nameof(element));","typeGuard":"bool CanUseAttachedChromeProps(IInputElement e) => e is DependencyObject;","tryCatchPattern":"try { var v = WindowChrome.GetIsHitTestVisibleInChrome(el); }\ncatch (ArgumentException) { /* element is not a DependencyObject */ }","preventionTips":["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."],"tags":["wpf","windowchrome","attached-property","argument"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}