dotnet/wpf · error · NotSupportedException

throw new…

Error message

throw new NotSupportedException(SR.RangeActionsNotSupported);

What it means

CompositeCollectionView's ValidateCollectionChangedEventArgs rejects Add notifications that carry more than one new item. Range Add actions are not supported by this view, so a multi-item Add throws NotSupportedException.

Solutions

  1. Raise one Add notification per item instead of a batched range Add.
  2. If batch semantics are needed, fall back to Reset only when the collection is fully cleared (see CompositeCollectionResetOnlyOnClear).
  3. Use ObservableCollection's single-item Add (or insert items in a loop) rather than a range Add helper.

Example fix

// before
var e = new NotifyCollectionChangedEventArgs(Add, changedItems.ToList()); // range

// after
foreach (var item in changedItems)
    OnCollectionChanged(new NotifyCollectionChangedEventArgs(Add, item));
Defensive patterns

Strategy: validation

Validate before calling

if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Count != 1)
    SplitIntoSingleItemNotifications(e);

Try / catch

try { view.ProcessCollectionChanged(e); }
catch (NotSupportedException) { RebuildView(); }

Prevention

When it happens

Trigger: A source collection raises CollectionChanged with Action=Add and NewItems.Count > 1 (e.g. AddRange implementations that batch multiple items into one notification).

Common situations: Using custom 'AddRange' observable collections that fire a single batched Add event; interop with sources that emit range notifications.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs:1434

        // error CS0649: Warning as Error: Field '..._traceLog' is never assigned to, and will always have its default value null
        private void InitializeTraceLog()
        {
            _traceLog = new TraceLog(20);
        }

        private void TraceContainerCollectionChange(object sender, NotifyCollectionChangedAction action, object oldItem, object newItem)
        {
            _traceLog?.Add("ContainerCollectionChange from {0}  action = {1} oldItem = {2} newItem = {3}",
                                TraceLog.IdFor(sender), action, TraceLog.IdFor(oldItem), TraceLog.IdFor(newItem));
        }

        private void ValidateCollectionChangedEventArgs(NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    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;

View on GitHub (pinned to 81131a70a4)