dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

ListCollectionView.Filter cannot be changed while an AddNew or EditItem transaction is open. The view blocks sort/filter/group mutations during a pending add/edit transaction because they would force a refresh and invalidate the transaction state.

Solutions

  1. Call CommitNew()/CommitEdit() (or CancelNew()/CancelEdit()) before assigning Filter
  2. Defer the filter assignment until the transaction completes (handle the collection's transaction end)
  3. Use a CollectionView DeferRefresh scope set up outside the transaction
  4. Use ICollectionView.Refresh or a fresh filter only when IsAddingNew and IsEditingItem are both false

Example fix

// before
collectionView.Filter = o => Matches(o); // throws during AddNew
// after
if (!collectionView.IsAddingNew && !(collectionView is IEditableCollectionView ecv && ecv.IsEditingItem))
    collectionView.Filter = o => Matches(o);
Defensive patterns

Strategy: validation

Validate before calling

var ecv = collectionView as IEditableCollectionView;
bool blocked = collectionView.IsAddingNew || (ecv != null && ecv.IsEditingItem);
if (!blocked) collectionView.Filter = myFilter;

Try / catch

try { collectionView.Filter = myFilter; }
catch (InvalidOperationException)
{
    // defer until the AddNew/EditItem transaction commits
}

Prevention

When it happens

Trigger: Setting the Filter property (ICollectionView.Filter) after calling AddNew() or EditItem() without first calling CommitNew/CommitEdit/CancelNew/CancelEdit.

Common situations: User-driven filtering (e.g. a search box handler) firing while a DataGrid row is in add/edit mode; a filter reapplied on a timer or property-change event while a new-item row is active.

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/44a13cbb6a10890f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:437

        /// item is suitable for inclusion in the view.
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// Simpler implementations do not support filtering and will throw a NotSupportedException.
        /// Use <seealso cref="CanFilter"/> property to test if filtering is supported before
        /// assigning a non-null value.
        /// </exception>
        public override Predicate<object> Filter
        {
            get
            {
                return base.Filter;
            }
            set
            {
                if (AllowsCrossThreadChanges)
                    VerifyAccess();
                if (IsAddingNew || IsEditingItem)
                    throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Filter"));

                base.Filter = value;
            }
        }

        #endregion ICollectionView

        /// <summary>
        /// Set a custom comparer to sort items using an object that implements IComparer.
        /// </summary>
        /// <remarks>
        /// Setting the Sort criteria has no immediate effect,
        /// an explicit <seealso cref="CollectionView.Refresh"/> call by the app is required.
        /// Note: Setting the custom comparer object will clear previously set <seealso cref="CollectionView.SortDescriptions"/>.
        /// </remarks>
        public IComparer CustomSort
        {
            get { return _customSort; }

View on GitHub (pinned to 81131a70a4)