{"record":{"id":"2afb6f00c5004198","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedforview-addnew-2afb6f","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedForView, \"AddNew\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedForView, \"AddNew\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":569,"sourceCode":"        /// <summary>\n        /// Add a new item to the underlying collection.  Returns the new item.\n        /// After calling AddNew and changing the new item as desired, either\n        /// <seealso cref=\"CommitNew\"/> or <seealso cref=\"CancelNew\"/> should be\n        /// called to complete the transaction.\n        /// </summary>\n        public object AddNew()\n        {\n            VerifyRefreshNotDeferred();\n\n            if (IsEditingItem)\n            {\n                CommitEdit();   // implicitly close a previous EditItem\n            }\n\n            CommitNew();        // implicitly close a previous AddNew\n\n            if (!CanAddNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, \"AddNew\"));\n\n            object newItem = null;\n            BindingOperations.AccessCollection(InternalList,\n                () =>\n                {\n                    ProcessPendingChanges();\n\n                    _newItemIndex = -2; // this is a signal that the next ItemAdded event comes from AddNew\n                    newItem = InternalList.AddNew();\n                },\n                true);\n\n            Debug.Assert(_newItemIndex != -2 && newItem == _newItem, \"AddNew did not raise expected events\");\n\n            MoveCurrentTo(newItem);\n\n            ISupportInitialize isi = newItem as ISupportInitialize;\n            isi?.BeginInit();","sourceCodeStart":551,"sourceCodeEnd":587,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L551-L587","documentation":"BindingListCollectionView.AddNew() throws this InvalidOperationException when the view does not currently permit adding a new item. AddNew first implicitly closes any pending CommitEdit/CommitNew transactions, then checks the CanAddNew flag; if the bound collection is not IBindingList with AllowNew support (or filtering/sorting state makes a new item unplaceable), the operation is rejected.","triggerScenarios":"Calling AddNew() on a BindingListCollectionView whose source collection is not IBindingList, whose AllowNew property is false, or whose custom CanAddNew logic (e.g. AddNew canPlaceNewItem filter) returns false.","commonSituations":"Binding an ItemsControl to a plain List<T> or ObservableCollection<T> (not BindingList<T>) and calling AddNew from an 'Add' button; a DataGrid in edit mode issuing implicit AddNew; a filter predicate rejecting the placeholder position for a new item.","solutions":["Check the view's CanAddNew property before calling AddNew and disable/redirect the Add action when false.","Use a source collection implementing IBindingList with AllowNew == true (e.g. BindingList<T> or a DataView).","Clear any active EditItem/AddNew transaction first; a pending transaction is implicitly closed, but the view may still report CanAddNew false due to filter state.","If a filter is active, verify your filter allows the position where the new item would be inserted, or remove the filter before adding."],"exampleFix":"// before\n((BindingListCollectionView)view).AddNew();\n\n// after\nvar blcv = (BindingListCollectionView)view;\nif (!blcv.CanAddNew) throw new NotSupportedException(\"Collection does not allow adding.\");\nblcv.AddNew();","handlingStrategy":"validation","validationCode":"// C#\nvar blcv = view as BindingListCollectionView;\nif (blcv == null || !blcv.CanAddNew) throw new InvalidOperationException(\"Cannot add new item on this view.\");","typeGuard":"bool CanAddSafely(ICollectionView v) => v is BindingListCollectionView b && b.CanAddNew;","tryCatchPattern":null,"preventionTips":["Bind to IBindingList sources (BindingList<T>) when you need AddNew.","Always check CanAddNew before AddNew.","Close open edit transactions before starting an add."],"tags":["wpf","data-binding","invalid-state"],"backgroundTag":"unsupported-operation","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"}