dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringAddOrEdit…

Error message

SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "DeferRefresh")

What it means

CollectionView.DeferRefresh throws InvalidOperationException when an AddNew or EditItem transaction is open (IsAddingNew/IsEditingItem). Deferring refresh during add/edit would freeze the view in a state that cannot reflect the pending item changes, so the call is rejected.

Solutions

  1. Commit or cancel the pending add/edit (CommitNew/CancelNew, CommitEdit/CancelEdit) before DeferRefresh
  2. Reorder logic: finish the edit transaction first, then open the defer scope around the property changes
  3. Check IsAddingNew/IsEditingItem and skip deferring when a transaction is open

Example fix

// before
using (collectionView.DeferRefresh()) { /* change filter */ } // throws during add/edit
// after
if (ecv.IsAddingNew) ecv.CommitNew();
if (ecv.IsEditingItem) ecv.CommitEdit();
using (collectionView.DeferRefresh()) { /* change filter */ }
Defensive patterns

Strategy: validation

Validate before calling

var ecv = (IEditableCollectionView)view;
if (ecv.IsAddingNew || ecv.IsEditingItem) ecv.CommitEdit();
using (view.DeferRefresh()) { /* batch changes */ }

Type guard

bool canDefer = view is IEditableCollectionView ecv && !ecv.IsAddingNew && !ecv.IsEditingItem;

Try / catch

try { defer = view.DeferRefresh(); }
catch (InvalidOperationException) { ecv.CancelEdit(); ecv.CancelNew(); defer = view.DeferRefresh(); }

Prevention

When it happens

Trigger: Calling DeferRefresh() (to batch filter/sort description changes) while IsAddingNew or IsEditingItem is true on the same view's IEditableCollectionView implementation.

Common situations: Wrapping Filter/SortDescriptions updates in a defer block from an event handler that fires while a DataGrid row add/edit is in progress; batch update helpers that always call DeferRefresh unconditionally.

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/33f1394ed224d936. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs:327

                VerifyAccess();

            RefreshOverride();

            SetFlag(CollectionViewFlags.NeedsRefresh, false);
        }

        /// <summary>
        /// Enter a Defer Cycle.
        /// Defer cycles are used to coalesce changes to the ICollectionView.
        /// </summary>
        public virtual IDisposable DeferRefresh()
        {
            if (AllowsCrossThreadChanges)
                VerifyAccess();

            IEditableCollectionView ecv = this as IEditableCollectionView;
            if (ecv != null && (ecv.IsAddingNew || ecv.IsEditingItem))
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "DeferRefresh"));

            ++ _deferLevel;
            return new DeferHelper(this);
        }

        /// <summary>
        /// Return the "current item" for this view
        /// </summary>
        /// <remarks>
        /// Only wrapper classes (those that pass currency handling calls to another internal
        /// CollectionView) should override CurrentItem; all other derived classes
        /// should use SetCurrent() to update the current values stored in the base class.
        /// </remarks>
        public virtual object CurrentItem
        {
            get
            {
                VerifyRefreshNotDeferred();

View on GitHub (pinned to 81131a70a4)