dotnet/wpf · error · InvalidOperationException

SR.CannotSelectNotSelectableItem

Error message

SR.CannotSelectNotSelectableItem

What it means

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.

Solutions

  1. Check Selector.ItemGetIsSelectable (or the item's IsSelectable/IsEnabled state) before assigning the selection
  2. Remove IsSelectable=false from the item, or pick a selectable representative item
  3. Guard the ViewModel so it never sets SelectedItem to a non-selectable model

Example fix

// before
listBox.SelectedItem = newItem; // newItem is non-selectable -> throws
// after
if (Selector.ItemGetIsSelectable(newItem))
    listBox.SelectedItem = newItem;
Defensive patterns

Strategy: validation

Validate before calling

if (!Selector.ItemGetIsSelectable(candidate))
    return; // skip or pick another item

Type guard

static bool IsSelectable(object item) =>
    item is DependencyObject d && Selector.ItemGetIsSelectable(d);

Try / catch

try
{
    listBox.SelectedItem = candidate;
}
catch (InvalidOperationException ex)
{
    // item is non-selectable; clear selection instead
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/bd87fc749594b122. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Selector.cs:1430

        #endregion

        #region Private Methods

        /// <summary>
        /// Adds/Removes the given item to the collection.  Assumes the item is in the collection.
        /// </summary>
        private void SetSelectedHelper(object item, FrameworkElement UI, bool selected)
        {
            Debug.Assert(!SelectionChange.IsActive, "SelectionChange is already active -- use SelectionChange.Select or Unselect");

            bool selectable;

            selectable = ItemGetIsSelectable(item);

            if (!selectable && selected)
            {
                throw new InvalidOperationException(SR.CannotSelectNotSelectableItem);
            }

            SelectionChange.Begin();
            try
            {
                ItemInfo info = NewItemInfo(item, UI);

                if (selected)
                {
                    SelectionChange.Select(info, true /* assumeInItemsCollection */);
                }
                else
                {
                    SelectionChange.Unselect(info);
                }
            }
            finally
            {

View on GitHub (pinned to 81131a70a4)