dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringAddOrEdit…

Error message

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

What it means

The CustomFilter setter throws InvalidOperationException when the view is in the middle of an AddNew transaction (IsAddingNew) or an item edit transaction (IsEditingItem). Structural view changes like changing the filter are not allowed during add/edit, so WPF rejects them with MemberNotAllowedDuringAddOrEdit naming 'CustomFilter'.

Solutions

  1. Commit or cancel the current transaction first: call CommitNew()/CancelNew() (and CommitEdit/CancelEdit) before setting CustomFilter.
  2. Check IsAddingNew / IsEditingItem before assigning CustomFilter and defer the change until the transaction completes.
  3. Set the filter before the user begins adding/editing rows (e.g. at view construction).

Example fix

// before
view.CustomFilter = "Amount > 100"; // may throw during row edit

// after
if (view.IsAddingNew) view.CommitNew();
if (view.IsEditingItem) view.CommitEdit();
view.CustomFilter = "Amount > 100";
Defensive patterns

Strategy: validation

Validate before calling

if (view.IsAddingNew) view.CommitNew();
if (view.IsEditingItem) view.CommitEdit();
view.CustomFilter = filter;

Try / catch

try { view.CustomFilter = filter; }
catch (InvalidOperationException ex) when (ex.Message.Contains("edit"))
{
    pendingFilter = filter; // apply after transaction completes
}

Prevention

When it happens

Trigger: Setting BindingListCollectionView.CustomFilter while CollectionView.AddNew/AddNew has an outstanding new item, or while EditItem has not been committed/cancelled (e.g. from a UI event while the DataGrid row is in edit mode).

Common situations: Applying or clearing a filter from a toolbar button while the grid's current row is being edited; filter logic in response to user input firing during a pending AddNew; automation/tests that change the filter without ending edit transactions.

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/4691a00c12544dbc. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs:306

        /// <summary>
        /// Gets or sets the filter to be used to exclude items from the collection of items returned by the data source .
        /// </summary>
        /// <remarks>
        /// Before assigning, test if this CollectionView supports custom filtering
        /// <seealso cref="CanCustomFilter"/>.
        /// The actual syntax depends on the implementer of IBindingListView. ADO's DataView is
        /// a common example, see System.Data.DataView.RowFilter for its supported
        /// filter expression syntax.
        /// </remarks>
        public string CustomFilter
        {
            get { return _customFilter; }
            set
            {
                if (!CanCustomFilter)
                    throw new NotSupportedException(SR.BindingListCannotCustomFilter);
                if (IsAddingNew || IsEditingItem)
                    throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "CustomFilter"));
                if (AllowsCrossThreadChanges)
                    VerifyAccess();

                _customFilter = value;

                RefreshOrDefer();
            }
        }

        /// <summary>
        /// Test if this CollectionView supports custom filtering before assigning
        /// a filter string to <seealso cref="CustomFilter"/>.
        /// </summary>
        public bool CanCustomFilter
        {
            get
            {
                return ((_blv != null) && _blv.SupportsFiltering);

View on GitHub (pinned to 81131a70a4)