dotnet/wpf · error · InvalidOperationException

SR.CannotMoveToUnknownPosition

Error message

SR.CannotMoveToUnknownPosition

What it means

A Move notification with NewStartingIndex < 0 (an 'unknown' position, value -1) throws InvalidOperationException(SR.CannotMoveToUnknownPosition) in ValidateCollectionChangedEventArgs. The view cannot apply a move when the notification does not specify the destination index.

Solutions

  1. Ensure the Move notification always carries a valid NewStartingIndex >= 0.
  2. Verify the source index exists before calling Move; never propagate a -1 from a failed IndexOf into Move.
  3. If the destination is genuinely unknown, use Reset + Refresh instead of Move.

Example fix

// before
int dest = list.IndexOf(item); // -1 if not found
list.Move(list.IndexOf(x), dest); // throws CannotMoveToUnknownPosition

// after
int dest = list.IndexOf(item);
if (dest >= 0) list.Move(oldIndex, dest);
Defensive patterns

Strategy: validation

Validate before calling

int dest = list.IndexOf(item);
if (dest >= 0 && oldIndex >= 0) list.Move(oldIndex, dest);

Try / catch

try { list.Move(oldIndex, dest); } catch (InvalidOperationException) { view.Refresh(); }

Prevention

When it happens

Trigger: Source collection raises NotifyCollectionChangedAction.Move with NewStartingIndex == -1 (the 'unknown index' sentinel), typically constructed with a constructor that omits the index or a custom notifier that fails to set it.

Common situations: Hand-rolled collection code calling the ObservableCollection.Move overload incorrectly or building MoveEventArgs without an index; moving an item whose position could not be determined (e.g. after a search that returned -1) and passing that -1 through.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs:2450

                    if (e.NewItems.Count != 1)
                        throw new NotSupportedException(SR.RangeActionsNotSupported);
                    break;

                case NotifyCollectionChangedAction.Remove:
                    if (e.OldItems.Count != 1)
                        throw new NotSupportedException(SR.RangeActionsNotSupported);
                    break;

                case NotifyCollectionChangedAction.Replace:
                    if (e.NewItems.Count != 1 || e.OldItems.Count != 1)
                        throw new NotSupportedException(SR.RangeActionsNotSupported);
                    break;

                case NotifyCollectionChangedAction.Move:
                    if (e.NewItems.Count != 1)
                        throw new NotSupportedException(SR.RangeActionsNotSupported);
                    if (e.NewStartingIndex < 0)
                        throw new InvalidOperationException(SR.CannotMoveToUnknownPosition);
                    break;

                case NotifyCollectionChangedAction.Reset:
                    break;

                default:
                    throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, e.Action));
            }
        }

        /// <summary>
        /// Helper to raise a PropertyChanged event  />).
        /// </summary>
        private void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

View on GitHub (pinned to 81131a70a4)