{"record":{"id":"44a13cbb6a10890f","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-filter","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Filter\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"Filter\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":437,"sourceCode":"        /// item is suitable for inclusion in the view.\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">\n        /// Simpler implementations do not support filtering and will throw a NotSupportedException.\n        /// Use <seealso cref=\"CanFilter\"/> property to test if filtering is supported before\n        /// assigning a non-null value.\n        /// </exception>\n        public override Predicate<object> Filter\n        {\n            get\n            {\n                return base.Filter;\n            }\n            set\n            {\n                if (AllowsCrossThreadChanges)\n                    VerifyAccess();\n                if (IsAddingNew || IsEditingItem)\n                    throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Filter\"));\n\n                base.Filter = value;\n            }\n        }\n\n        #endregion ICollectionView\n\n        /// <summary>\n        /// Set a custom comparer to sort items using an object that implements IComparer.\n        /// </summary>\n        /// <remarks>\n        /// Setting the Sort criteria has no immediate effect,\n        /// an explicit <seealso cref=\"CollectionView.Refresh\"/> call by the app is required.\n        /// Note: Setting the custom comparer object will clear previously set <seealso cref=\"CollectionView.SortDescriptions\"/>.\n        /// </remarks>\n        public IComparer CustomSort\n        {\n            get { return _customSort; }","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L419-L455","documentation":"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.","triggerScenarios":"Setting the Filter property (ICollectionView.Filter) after calling AddNew() or EditItem() without first calling CommitNew/CommitEdit/CancelNew/CancelEdit.","commonSituations":"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.","solutions":["Call CommitNew()/CommitEdit() (or CancelNew()/CancelEdit()) before assigning Filter","Defer the filter assignment until the transaction completes (handle the collection's transaction end)","Use a CollectionView DeferRefresh scope set up outside the transaction","Use ICollectionView.Refresh or a fresh filter only when IsAddingNew and IsEditingItem are both false"],"exampleFix":"// before\ncollectionView.Filter = o => Matches(o); // throws during AddNew\n// after\nif (!collectionView.IsAddingNew && !(collectionView is IEditableCollectionView ecv && ecv.IsEditingItem))\n    collectionView.Filter = o => Matches(o);","handlingStrategy":"validation","validationCode":"var ecv = collectionView as IEditableCollectionView;\nbool blocked = collectionView.IsAddingNew || (ecv != null && ecv.IsEditingItem);\nif (!blocked) collectionView.Filter = myFilter;","typeGuard":null,"tryCatchPattern":"try { collectionView.Filter = myFilter; }\ncatch (InvalidOperationException)\n{\n    // defer until the AddNew/EditItem transaction commits\n}","preventionTips":["Always close AddNew/EditItem transactions before mutating view settings","Centralize filter changes in one method that checks IsAddingNew/IsEditingItem first","In DataGrid UIs, apply filter changes only on commit/cancel of row transactions"],"tags":["wpf","data-binding","invalidoperationexception","listcollectionview","filter"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}