dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedForView, "CommitEdit")

Error message

SR.Format(SR.MemberNotAllowedForView, "CommitEdit")

What it means

ItemCollection.CommitEdit ends an edit transaction on the current item, but only the active view can perform it. When the collection's view does not implement IEditableCollectionView, the method throws InvalidOperationException with MemberNotAllowedForView("CommitEdit"). This guards IEditableCollectionViewAddNewItem/IEditableCollectionView members that are only meaningful for editable views.

Solutions

  1. Check the CanCommitEdit property (or view is IEditableCollectionView) before calling CommitEdit
  2. Cast the view: if (items.CollectionView is IEditableCollectionView ecv) ecv.CommitEdit();
  3. Ensure the ItemsSource is a type whose default view supports editing (e.g. ObservableCollection, or use a CollectionViewSource with an editable source)

Example fix

// before
items.CommitEdit();
// after
if (items.CanCommitEdit) { items.CommitEdit(); }
Defensive patterns

Strategy: validation

Validate before calling

if (items.CanCommitEdit) items.CommitEdit();

Type guard

bool canCommit = items.CollectionView is IEditableCollectionView ecv && ecv.CanCommitEdit;

Try / catch

try { items.CommitEdit(); } catch (InvalidOperationException ex) { /* view does not support edit transactions */ }

Prevention

When it happens

Trigger: Calling ItemCollection.CommitEdit() when the underlying ICollectionView (e.g. a plain ListCollectionView wrapper over a non-editable source, or a custom view) does not implement IEditableCollectionView — i.e. CanCommitEdit is false.

Common situations: Binding an ItemsControl to a source whose default view lacks edit-transaction support, then calling CommitEdit directly on Items or ItemCollection in code; porting code written against a DataGrid/BindingList-based source to a simple IEnumerable source.

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/36a7022258a4a992. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs:1100

            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "EditItem"));
            }
        }

        /// <summary>
        /// Complete the transaction started by <seealso cref="IEditableCollectionView.EditItem"/>.
        /// The pending changes (if any) to the item are committed.
        /// </summary>
        void    IEditableCollectionView.CommitEdit()
        {
            IEditableCollectionView ecv = _collectionView as IEditableCollectionView;
            if (ecv != null)
            {
                ecv.CommitEdit();
            }
            else
            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "CommitEdit"));
            }
        }

        /// <summary>
        /// Complete the transaction started by <seealso cref="IEditableCollectionView.EditItem"/>.
        /// The pending changes (if any) to the item are discarded.
        /// </summary>
        void    IEditableCollectionView.CancelEdit()
        {
            IEditableCollectionView ecv = _collectionView as IEditableCollectionView;
            if (ecv != null)
            {
                ecv.CancelEdit();
            }
            else
            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "CancelEdit"));
            }

View on GitHub (pinned to 81131a70a4)