{"record":{"id":"49a1523741e3ded6","repo":"dotnet/wpf","slug":"selection-container-does-not-support-multiple-selection","errorCode":null,"errorMessage":"Selection container does not support multiple selection.","messagePattern":"Selection container does not support multiple selection\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsButton.cs","lineNumber":366,"sourceCode":"            {\n                return false;\n            }\n        }\n\n        #endregion Selection Pattern\n\n        #region SelectionItem Pattern\n\n        // Selects this element\n        void ISelectionItemProvider.Select()\n        {\n            Invoke();\n        }\n\n        // Adds this element to the selection\n        void ISelectionItemProvider.AddToSelection()\n        {\n            throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);\n        }\n\n        // Removes this element from the selection\n        void ISelectionItemProvider.RemoveFromSelection()\n        {\n            throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);\n        }\n\n        // True if this element is part of the the selection\n        bool ISelectionItemProvider.IsSelected\n        {\n            get\n            {\n                return ToggleState == ToggleState.On ? true : false;\n            }\n        }\n\n        // Returns the container for this element","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsButton.cs#L348-L384","documentation":"ISelectionItemProvider.AddToSelection on the WindowsButton proxy unconditionally throws InvalidOperationException('Selection container does not support multiple selection.'). A single button element is not part of a multi-select container, so adding it to a selection is meaningless; the proxy models this by throwing SR.DoesNotSupportMultipleSelection. The same throw exists in RemoveFromSelection.","triggerScenarios":"A UIA client obtains SelectionItemPattern from a WindowsButton element and calls AddToSelection() on the pattern.","commonSituations":"Automation scripts generically calling AddToSelection on any SelectionItemPattern element; test frameworks that blanket-exercise every supported pattern on controls; ported code that assumed a list-box-like container.","solutions":["Do not call AddToSelection on buttons; instead call the InvokePattern Invoke() method (the proxy routes selection semantics to Invoke).","Check the element's SelectionContainer to confirm it actually supports multiple selection before calling AddToSelection.","Catch InvalidOperationException around AddToSelection/RemoveFromSelection and fall back to Invoke.","If toggle state is what you need, use TogglePattern where the control exposes it instead of SelectionItemPattern."],"exampleFix":"// before\nvar selItem = button.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;\nselItem.AddToSelection();\n// after\nvar invoke = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;\nif (invoke != null) invoke.Invoke();","handlingStrategy":"validation","validationCode":"// C# (UIA client)\nvar selItem = element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;\nvar container = selItem?.Current.SelectionContainer;\nbool multiSelect = container != null &&\n    (container.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern)?.Current.CanSelectMultiple == true;\n// only call AddToSelection when multiSelect is true; otherwise use InvokePattern","typeGuard":null,"tryCatchPattern":"try { selItem.AddToSelection(); }\ncatch (InvalidOperationException)\n{\n    // unsupported; fall back to InvokePattern\n    ((InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern)).Invoke();\n}","preventionTips":["Treat SelectionItemPattern on buttons as Invoke-only: select via Invoke, not AddToSelection.","Verify the SelectionContainer supports CanSelectMultiple before calling Add/Remove.","Prefer TogglePattern for buttons with on/off state.","Avoid blanket pattern-method exercise in generic UIA test harnesses."],"tags":["uiautomation","selection-item-pattern","invalid-operation","winforms"],"backgroundTag":"unsupported-operation","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"}