{"record":{"id":"bd87fc749594b122","repo":"dotnet/wpf","slug":"sr-cannotselectnotselectableitem","errorCode":null,"errorMessage":"SR.CannotSelectNotSelectableItem","messagePattern":"SR\\.CannotSelectNotSelectableItem","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Selector.cs","lineNumber":1430,"sourceCode":"\n        #endregion\n\n        #region Private Methods\n\n        /// <summary>\n        /// Adds/Removes the given item to the collection.  Assumes the item is in the collection.\n        /// </summary>\n        private void SetSelectedHelper(object item, FrameworkElement UI, bool selected)\n        {\n            Debug.Assert(!SelectionChange.IsActive, \"SelectionChange is already active -- use SelectionChange.Select or Unselect\");\n\n            bool selectable;\n\n            selectable = ItemGetIsSelectable(item);\n\n            if (!selectable && selected)\n            {\n                throw new InvalidOperationException(SR.CannotSelectNotSelectableItem);\n            }\n\n            SelectionChange.Begin();\n            try\n            {\n                ItemInfo info = NewItemInfo(item, UI);\n\n                if (selected)\n                {\n                    SelectionChange.Select(info, true /* assumeInItemsCollection */);\n                }\n                else\n                {\n                    SelectionChange.Unselect(info);\n                }\n            }\n            finally\n            {","sourceCodeStart":1412,"sourceCodeEnd":1448,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Selector.cs#L1412-L1448","documentation":"Selector throws this InvalidOperationException when an attempt is made to select an item that ItemGetIsSelectable considers non-selectable - for example an item whose container has IsSelectable=false or is in a state where selection is invalid (e.g. disabled containers or placeholders). Selecting a non-selectable item would leave the control in an inconsistent state, so the public selection API rejects it eagerly.","triggerScenarios":"Calling SelectedItem = x / SelectedIndex = i / SelectedItems.Add(x) where x's container has IsSelectable=false; selecting a placeholder or header item; data-bound IsSelectable returning false for the target item.","commonSituations":"Selecting group headers or separator-like items programmatically; selecting the newly added 'add new item' row; binding SelectedItem in MVVM to an item that the container style marks non-selectable.","solutions":["Check Selector.ItemGetIsSelectable (or the item's IsSelectable/IsEnabled state) before assigning the selection","Remove IsSelectable=false from the item, or pick a selectable representative item","Guard the ViewModel so it never sets SelectedItem to a non-selectable model"],"exampleFix":"// before\nlistBox.SelectedItem = newItem; // newItem is non-selectable -> throws\n// after\nif (Selector.ItemGetIsSelectable(newItem))\n    listBox.SelectedItem = newItem;","handlingStrategy":"validation","validationCode":"if (!Selector.ItemGetIsSelectable(candidate))\n    return; // skip or pick another item","typeGuard":"static bool IsSelectable(object item) =>\n    item is DependencyObject d && Selector.ItemGetIsSelectable(d);","tryCatchPattern":"try\n{\n    listBox.SelectedItem = candidate;\n}\ncatch (InvalidOperationException ex)\n{\n    // item is non-selectable; clear selection instead\n}","preventionTips":["Check selectability before programmatic selection","Don't bind SelectedItem to header/placeholder items","Keep IsSelectable=true for items exposed as selection targets"],"tags":["wpf","selector","selection","invalidoperation"],"backgroundTag":"invalid-state-transition","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"}