{"record":{"id":"c0433880f49800b1","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-relativeto-gettype-c04338","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, relativeTo.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, relativeTo\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs","lineNumber":1162,"sourceCode":"            get\n            {\n                VerifyAccess();\n                return _stylusButtonCollection;\n            }\n        }\n\n        /////////////////////////////////////////////////////////////////////\n        /// <summary>\n        ///     Calculates the position of the stylus relative to a particular element.\n        /// </summary>\n        internal override Point GetPosition(IInputElement relativeTo)\n        {\n            VerifyAccess();\n\n            // Validate that relativeTo is either a UIElement, a ContentElement or a UIElement3D.\n            if (relativeTo != null && !InputElement.IsValid(relativeTo))\n            {\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, relativeTo.GetType()));\n            }\n\n            PresentationSource relativePresentationSource = null;\n\n            if (relativeTo != null)\n            {\n                DependencyObject dependencyObject = relativeTo as DependencyObject;\n                DependencyObject containingVisual = InputElement.GetContainingVisual(dependencyObject);\n                if (containingVisual != null)\n                {\n                    relativePresentationSource = PresentationSource.CriticalFromVisual(containingVisual);\n                }\n            }\n            else\n            {\n                if (_inputSource != null)\n                {\n                    relativePresentationSource = _inputSource;","sourceCodeStart":1144,"sourceCodeEnd":1180,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs#L1144-L1180","documentation":"WispStylusDevice.GetPosition (WispStylusDevice.cs:1162) throws InvalidOperationException(SR.Invalid_IInputElement) when the relativeTo argument is non-null and not a valid IInputElement (UIElement, ContentElement, or UIElement3D). GetPosition needs a supported input element to compute the stylus coordinate space.","triggerScenarios":"Calling stylusDevice.GetPosition(relativeTo) with a raw Visual, Visual3D, or other DependencyObject that does not implement IInputElement properly.","commonSituations":"Passing a DrawingVisual or adorner-layer visual as the reference; variables widened to DependencyObject/Visual during refactoring; passing elements from third-party visual frameworks.","solutions":["Pass a UIElement, ContentElement, or UIElement3D as relativeTo (e.g. the hosting control instead of the raw visual).","Validate InputElement.IsValid(relativeTo) before calling GetPosition.","Pass null to get coordinates relative to the presentation source and convert manually if needed."],"exampleFix":"// before\nvar pt = stylusDevice.GetPosition(myDrawingVisual); // throws\n// after\nif (InputElement.IsValid(myDrawingVisual)) { var pt = stylusDevice.GetPosition(myDrawingVisual); }","handlingStrategy":"validation","validationCode":"if (relativeTo != null && !InputElement.IsValid(relativeTo))\n    throw new ArgumentException(\"relativeTo must be UIElement, ContentElement, or UIElement3D\");","typeGuard":"static bool IsValidPositionReference(IInputElement e) => e == null || e is UIElement || e is ContentElement || e is UIElement3D;","tryCatchPattern":"try { var pt = stylusDevice.GetPosition(relativeTo); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"IInputElement\")) { /* pass null and transform manually via the hosting UIElement */ }","preventionTips":["Use the hosting control (UIElement) as the coordinate reference, not raw visuals","Validate relativeTo with InputElement.IsValid before calling","Type reference parameters as UIElement to catch mistakes at compile time"],"tags":["wpf","stylus","coordinates","invalidoperation"],"backgroundTag":"incompatible-source-type","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"}