{"record":{"id":"7c93e62f4cb356dd","repo":"dotnet/wpf","slug":"can-only-call-selectall-when-canselectmultipleitems-is-true","errorCode":null,"errorMessage":"Can only call SelectAll when CanSelectMultipleItems is true.","messagePattern":"Can only call SelectAll when CanSelectMultipleItems is true\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/MultiSelector.cs","lineNumber":87,"sourceCode":"            get\n            {\n                return ((SelectedItemCollection)SelectedItems).IsUpdatingSelectedItems;\n            }\n        }\n\n        /// <summary>\n        ///     Select all the items\n        /// Exceptions: InvalidOperationExcpetion if CanSelectMultipleItems is false\n        /// </summary>\n        public void SelectAll()\n        {\n            if (CanSelectMultipleItems)\n            {\n                SelectAllImpl();\n            }\n            else\n            {\n                throw new NotSupportedException(SR.MultiSelectorSelectAll);\n            }\n        }\n\n        /// <summary>\n        ///     Clears all of the selected items.\n        /// </summary>\n        public void UnselectAll()\n        {\n            UnselectAllImpl();\n        }\n\n        #endregion BulkSelection\n    }\n}\n\n","sourceCodeStart":69,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/MultiSelector.cs#L69-L103","documentation":"MultiSelector.SelectAll only works when the selector allows multiple selection; CanSelectMultipleItems is true only when SelectionMode is Extended or Multiple. Calling SelectAll in single-selection mode throws NotSupportedException because selecting all items is meaningless there.","triggerScenarios":"Calling SelectAll()/SelectAllCells() on a DataGrid or MultiSelector whose SelectionMode is Single (or while CanSelectMultipleItems is temporarily false during internal updates).","commonSituations":"Copy-pasting selection code between a ListBox (Extended default) and a DataGrid configured with SelectionMode=\"Single\", or calling SelectAll before the selection mode is applied.","solutions":["Set SelectionMode=\"Extended\" or \"Multiple\" on the control before calling SelectAll","Guard the call with if (selector.CanSelectMultipleItems)","In single-selection mode, select the desired item with SelectedIndex/SelectedItem instead"],"exampleFix":"// before\nif (dataGrid.SelectedIndex >= 0) dataGrid.SelectAll();\n// after\nif (dataGrid.CanSelectMultipleItems)\n    dataGrid.SelectAll();\nelse\n    dataGrid.SelectedIndex = 0;","handlingStrategy":"validation","validationCode":"if (selector.CanSelectMultipleItems) selector.SelectAll(); else selector.SelectedIndex = 0;","typeGuard":"static bool CanSelectAll(System.Windows.Controls.Primitives.MultiSelector s) => s.CanSelectMultipleItems;","tryCatchPattern":"try { selector.SelectAll(); }\ncatch (NotSupportedException) { /* SelectionMode is Single - handle single selection instead */ }","preventionTips":["Check CanSelectMultipleItems before SelectAll/SelectAllCells","Set SelectionMode explicitly (Extended/Multiple) when multi-select is intended","Remember SelectAll is semantically a no-op in single-select mode, hence the throw"],"tags":["wpf","datagrid","selection"],"backgroundTag":"unsupported-operation","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"}