{"record":{"id":"a9a0962d7e03faf6","repo":"dotnet/wpf","slug":"sr-textprovider-invalidchild","errorCode":null,"errorMessage":"SR.TextProvider_InvalidChild","messagePattern":"SR\\.TextProvider_InvalidChild","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/TextProviderWrapper.cs","lineNumber":56,"sourceCode":"        //------------------------------------------------------\n \n        #region Interface ITextProvider\n\n        public ITextRangeProvider [] GetSelection()\n        {\n            return (ITextRangeProvider [])ElementUtil.Invoke(_peer, new DispatcherOperationCallback(GetSelection), null);\n        }\n\n        public ITextRangeProvider [] GetVisibleRanges()\n        {\n            return (ITextRangeProvider[])ElementUtil.Invoke(_peer, new DispatcherOperationCallback(GetVisibleRanges), null);\n        }\n\n        public ITextRangeProvider RangeFromChild(IRawElementProviderSimple childElement)\n        {\n            if (!(childElement is ElementProxy))\n            {\n                throw new ArgumentException(SR.Format(SR.TextProvider_InvalidChild, \"childElement\"));\n            }\n\n            return (ITextRangeProvider)ElementUtil.Invoke(_peer, new DispatcherOperationCallback(RangeFromChild), childElement);\n        }\n\n        public ITextRangeProvider RangeFromPoint(Point screenLocation)\n        {\n            return (ITextRangeProvider)ElementUtil.Invoke(_peer, new DispatcherOperationCallback(RangeFromPoint), screenLocation);\n        }\n\n        public ITextRangeProvider DocumentRange \n        {\n            get\n            {\n                return (ITextRangeProvider)ElementUtil.Invoke(_peer, new DispatcherOperationCallback(GetDocumentRange), null);\n            }\n        }\n","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Automation/TextProviderWrapper.cs#L38-L74","documentation":"TextProviderWrapper.RangeFromChild wraps a peer's ITextRangeProvider.RangeFromChild call. It requires the childElement argument to be an ElementProxy (the wrapper's provider representation); any other IRawElementProviderSimple is rejected up front with ArgumentException and SR.TextProvider_InvalidChild before the call is dispatched to the peer's thread.","triggerScenarios":"Calling ITextRangeProvider.RangeFromChild (via UIA TextPattern.RangeFromChild) with a child element that was not obtained from this provider — e.g. an element from another provider/tree, a null reference, or a hand-built IRawElementProviderSimple that is not the ElementProxy this wrapper expects.","commonSituations":"Passing elements discovered in a different UIA tree or from another control into RangeFromChild; caching child elements across provider rebuilds so the proxy identity no longer matches; test code constructing mock providers.","solutions":["Only pass child elements obtained from the same provider's tree (e.g. via GetChildren or RangeFromChild of the same TextPattern).","Null-check and verify the element's runtime ID / provider identity matches the target provider before calling RangeFromChild.","Refresh child element references from the current provider after UI updates instead of reusing stale ones.","In client code catch ArgumentException from RangeFromChild and fall back to re-discovering the child."],"exampleFix":"// before\nvar child = otherPattern.GetSelection()[0]; // element from a different provider\nvar range = textPattern.RangeFromChild(child);\n// after\nvar child = textPatternDocument.GetChildren()[0]; // child of the same provider\nif (child != null)\n{\n    var range = textPattern.RangeFromChild(child);\n}","handlingStrategy":"type-guard","validationCode":"if (childElement == null) throw new ArgumentNullException(nameof(childElement));\nif (childElement is not ElementProxy) throw new ArgumentException(\"childElement must come from the same provider\", nameof(childElement));","typeGuard":"bool IsValidChild(IRawElementProviderSimple child) => child is ElementProxy;","tryCatchPattern":"try\n{\n    var range = textPattern.RangeFromChild(childElement);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"childElement\"))\n{\n    childElement = documentElement.FindFirst(TreeScope.Children, condition); // rediscover from same provider\n}","preventionTips":["Only pass child elements obtained from the same provider's tree","Refresh child element references after UI updates rather than caching","Verify element runtime IDs belong to the target document before RangeFromChild","Null-check child elements before calling TextPattern APIs"],"tags":["wpf","ui-automation","argumentexception","textpattern","invalid-child"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}