{"record":{"id":"4c8ca4b315196ecc","repo":"dotnet/wpf","slug":"sr-treescopeneedatleastone-cacherequest","errorCode":null,"errorMessage":"SR.TreeScopeNeedAtLeastOne","messagePattern":"SR\\.TreeScopeNeedAtLeastOne","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/CacheRequest.cs","lineNumber":303,"sourceCode":"        /// </summary>\n        /// <remarks>\n        /// At least one or more of of TreeScope.Element, TreeScope.Children or\n        /// TreeScope.Descendants must be specified.\n        /// \n        /// 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        }","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/CacheRequest.cs#L285-L321","documentation":"The CacheRequest.TreeScope property setter rejects a value of 0 (TreeScope.None). A cache request must target at least one part of the automation tree (the element itself, its children, or its descendants), so a zero scope is meaningless and throws ArgumentException.","triggerScenarios":"Assigning (TreeScope)0 or TreeScope.None to CacheRequest.TreeScope before or after activating the request.","commonSituations":"Computing scope flags dynamically from booleans and having all flags evaluate false; passing an unset/zero enum field into TreeScope; serializing/deserializing settings where scope defaulted to 0.","solutions":["Assign a scope that includes at least TreeScope.Element, TreeScope.Children, or TreeScope.Descendants","Validate computed scope values before assignment and fall back to TreeScope.Element if the combined value is 0","Guard user/config-driven scope values with a check for 0"],"exampleFix":"// before\nrequest.TreeScope = (TreeScope)flags; // flags == 0 -> ArgumentException\n// after\nif ((TreeScope)flags == 0) flags = TreeScope.Element;\nrequest.TreeScope = (TreeScope)flags;","handlingStrategy":"validation","validationCode":"if (scope == 0) scope = TreeScope.Element;\nrequest.TreeScope = scope;","typeGuard":"bool IsValidCacheScope(TreeScope s) => s != 0 && (s & ~(TreeScope.Element|TreeScope.Children|TreeScope.Descendants)) == 0;","tryCatchPattern":"try { request.TreeScope = scope; } catch (ArgumentException) { request.TreeScope = TreeScope.Element; }","preventionTips":["Always combine at least one scope flag","Default computed scopes to TreeScope.Element when empty","Validate config-driven scope values before assignment"],"tags":["uiautomation","treescope","argument-exception","caching"],"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"}