{"record":{"id":"8507e522b0e83b53","repo":"dotnet/wpf","slug":"can-only-call-selectall-when-selectionmode-is-multiple-or","errorCode":null,"errorMessage":"Can only call SelectAll when SelectionMode is Multiple or Extended.","messagePattern":"Can only call SelectAll when SelectionMode is Multiple or Extended\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListBox.cs","lineNumber":102,"sourceCode":"        //\n        //  Public Methods\n        //\n        //-------------------------------------------------------------------\n\n        #region Public Methods\n\n        /// <summary>\n        ///     Select all the items\n        /// </summary>\n        public void SelectAll()\n        {\n            if (CanSelectMultiple)\n            {\n                SelectAllImpl();\n            }\n            else\n            {\n                throw new NotSupportedException(SR.ListBoxSelectAllSelectionMode);\n            }\n        }\n\n        /// <summary>\n        ///     Clears all of the selected items.\n        /// </summary>\n        public void UnselectAll()\n        {\n            UnselectAllImpl();\n        }\n\n        /// <summary>\n        /// Causes the object to scroll into view.  If it is not visible, it is aligned either at the top or bottom of the viewport.\n        /// </summary>\n        /// <param name=\"item\"></param>\n        public void ScrollIntoView(object item)\n        {\n            if (ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListBox.cs#L84-L120","documentation":"ListBox.SelectAll only works when the list supports multi-selection. When SelectionMode is Single, CanSelectMultiple is false and the method throws NotSupportedException rather than silently selecting one item.","triggerScenarios":"Calling listBox.SelectAll() (or Items.SelectAll via keyboard automation) on a ListBox with SelectionMode=\"Single\" — the default.","commonSituations":"UI code written for multi-select lists reused on single-select lists; SelectionMode changed to Single by a designer/style while code still calls SelectAll; automated tests invoking SelectAllCtrl gestures.","solutions":["Check CanSelectMultiple before calling SelectAll and handle the single-select case explicitly.","Set SelectionMode to Multiple or Extended on the ListBox if multi-selection is the intended behavior.","If a single item must be chosen, use SelectedItem/SelectedIndex instead of SelectAll."],"exampleFix":"// before\nlistBox.SelectAll();\n// after\nif (listBox.SelectionMode == SelectionMode.Multiple || listBox.SelectionMode == SelectionMode.Extended)\n    listBox.SelectAll();\nelse\n    listBox.SelectedItem = listBox.Items.Count > 0 ? listBox.Items[0] : null;","handlingStrategy":"validation","validationCode":"if (!listBox.CanSelectMultiple)\n    throw new InvalidOperationException(\"SelectAll requires SelectionMode Multiple or Extended\");","typeGuard":"static bool CanSelectAll(ListBox lb) => lb.CanSelectMultiple;","tryCatchPattern":"try { listBox.SelectAll(); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"SelectAll\")) { listBox.SelectedItem = listBox.Items.Count > 0 ? listBox.Items[0] : null; }","preventionTips":["Guard SelectAll calls with CanSelectMultiple or an explicit SelectionMode check.","Keep SelectionMode in sync between XAML and code-behind expectations.","Write UI tests that exercise SelectAll against every SelectionMode used in the app."],"tags":["wpf","listbox","selection","unsupported-operation"],"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"}