dotnet/wpf · error · NotSupportedException

SR.UnexpectedCollectionChangeAction

Error message

SR.UnexpectedCollectionChangeAction

What it means

TreeView.OnItemsChanged only supports Add, Move, Remove, Replace and Reset notifications it handles explicitly; any other NotifyCollectionChangedAction reaching the default case throws NotSupportedException with SR.UnexpectedCollectionChangeAction. It signals that the source collection raised a change action the TreeView cannot process.

Solutions

  1. Use ObservableCollection<T> or a standard INotifyCollectionChanged implementation for ItemsSource.
  2. Fix the custom collection to only raise valid NotifyCollectionChangedAction values.
  3. Replace the whole ItemsSource instead of emitting exotic change actions from the source collection.

Example fix

// before
customCollection.RaiseCollectionChanged(NotifyCollectionChangedAction.Drag); // bogus action
// after
using (customCollection.SuppressNotifications())
    customCollection.RefreshItems(); // or raise Reset
Defensive patterns

Strategy: validation

Validate before calling

if (e.Action is NotifyCollectionChangedAction.Add or NotifyCollectionChangedAction.Move
    or NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Replace
    or NotifyCollectionChangedAction.Reset) { /* acceptable source */ }

Try / catch

try { treeView.ItemsSource = myCollection; }
catch (NotSupportedException ex) { /* collection raises unsupported actions; swap to ObservableCollection */ }

Prevention

When it happens

Trigger: Binding TreeView.ItemsSource to a collection whose INotifyCollectionChanged implementation raises an unexpected/invalid action value, or a custom ObservableCollection raising non-standard actions.

Common situations: Custom collection classes with hand-rolled CollectionChanged events; third-party bindable collections that misreport actions; framework version mismatches where a new action value appears.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TreeView.cs:439

                case NotifyCollectionChangedAction.Replace:
                    {
                        // If old item is selected - remove the selection
                        // Revisit the condition when we support duplicate items in Items collection: if e.OldItems[0] is the same as selected items we will unselect the selected item
                        object selectedItem = SelectedItem;
                        if ((selectedItem != null) && selectedItem.Equals(e.OldItems[0]))
                        {
                            ChangeSelection(selectedItem, _selectedContainer, false);
                        }
                    }
                    break;

                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Move:
                    break;

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

        private void SelectFirstItem()
        {
            object item;
            TreeViewItem container;
            bool selected = GetFirstItem(out item, out container);
            if (!selected)
            {
                item = SelectedItem;
                container = _selectedContainer;
            }

            ChangeSelection(item, container, selected);
        }

        private bool GetFirstItem(out object item, out TreeViewItem container)

View on GitHub (pinned to 81131a70a4)