{"record":{"id":"c279c7a7b8e4aba1","repo":"dotnet/wpf","slug":"sr-treescopeelementchildrendescendantsonly-cacherequest","errorCode":null,"errorMessage":"SR.TreeScopeElementChildrenDescendantsOnly","messagePattern":"SR\\.TreeScopeElementChildrenDescendantsOnly","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/CacheRequest.cs","lineNumber":308,"sourceCode":"        /// TreeScope.Parent and TreeScope.Ancestors are not supported.\n        /// </remarks>\n        public TreeScope TreeScope\n        {\n            get\n            {\n                return _scope;\n            }\n            \n            set\n            {\n                if (value == 0)\n                {\n                    throw new ArgumentException(SR.TreeScopeNeedAtLeastOne);\n                }\n\n                if ((value & ~(TreeScope.Element | TreeScope.Children | TreeScope.Descendants)) != 0)\n                {\n                    throw new ArgumentException(SR.TreeScopeElementChildrenDescendantsOnly);\n                }\n\n                lock (_instanceLock)\n                {\n                    CheckAccess();\n                    if (_scope != value)\n                    {\n                        _scope = value;\n                        Invalidate();\n                    }\n                }\n            }\n        }\n\n\n        /// <summary>\n        /// Indicates the view to use when prefetching relative nodes\n        /// </summary>","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/CacheRequest.cs#L290-L326","documentation":"The CacheRequest.TreeScope property setter only accepts Element, Children, and Descendants bits. Any other TreeScope flag (e.g. Parent, Ancestors, Subtree) makes the masked value non-zero and throws ArgumentException, because UI Automation caching cannot traverse upward or to the root.","triggerScenarios":"Assigning TreeScope.Subtree (which includes Ancestors), TreeScope.Parent, TreeScope.Ancestors, or any combination containing those bits to CacheRequest.TreeScope.","commonSituations":"Reusing TreeScope values intended for other UIA APIs (like Find with Subtree) in a CacheRequest; a generic scope picker in UI code that offers Parent/Ancestors options; converting between legacy TreeScope constants.","solutions":["Use only TreeScope.Element, TreeScope.Children, and TreeScope.Descendants (or combinations of those)","Replace TreeScope.Subtree with TreeScope.Element | TreeScope.Children | TreeScope.Descendants","Strip upward-scope bits before assignment: scope &= ~(TreeScope.Parent | TreeScope.Ancestors)","For ancestor data, issue separate element navigation (TreeWalker) instead of caching upward"],"exampleFix":"// before\nrequest.TreeScope = TreeScope.Subtree; // throws\n// after\nrequest.TreeScope = TreeScope.Element | TreeScope.Children | TreeScope.Descendants;","handlingStrategy":"validation","validationCode":"const TreeScope allowed = TreeScope.Element|TreeScope.Children|TreeScope.Descendants;\nscope &= allowed; if (scope == 0) scope = TreeScope.Element;","typeGuard":"bool IsCacheableScope(TreeScope s) => (s & ~(TreeScope.Element|TreeScope.Children|TreeScope.Descendants)) == 0 && s != 0;","tryCatchPattern":"try { request.TreeScope = scope; } catch (ArgumentException) { request.TreeScope = TreeScope.Element|TreeScope.Children; }","preventionTips":["Never use TreeScope.Parent/Ancestors/Subtree in CacheRequest","Replace Subtree with Element|Children|Descendants","Mask incoming scope values against allowed bits"],"tags":["uiautomation","treescope","argument-exception","caching"],"backgroundTag":"invalid-enum-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"}