dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringTransaction…

Error message

SR.Format(SR.MemberNotAllowedDuringTransaction, "NewItemPlaceholderPosition", "AddNew")

What it means

NewItemPlaceholderPosition cannot be changed while an AddNew transaction is in progress. When the value differs from the current one and IsAddingNew is true, the view throws InvalidOperationException naming AddNew as the blocking transaction.

Solutions

  1. Commit or cancel the AddNew transaction before changing NewItemPlaceholderPosition
  2. Set NewItemPlaceholderPosition before starting the AddNew transaction
  3. Skip the change when the requested value equals the current value (the throw only occurs when they differ)

Example fix

// before
collectionView.AddNew();
collectionView.NewItemPlaceholderPosition = NewItemPlaceholderPosition.First; // throws
// after
if (collectionView.IsAddingNew) collectionView.CommitNew();
collectionView.NewItemPlaceholderPosition = NewItemPlaceholderPosition.First;
Defensive patterns

Strategy: validation

Validate before calling

var ecv = collectionView as IEditableCollectionView;
if (value != collectionView.NewItemPlaceholderPosition &&
    !(ecv != null && ecv.IsAddingNew))
{
    collectionView.NewItemPlaceholderPosition = value;
}

Try / catch

try { collectionView.NewItemPlaceholderPosition = value; }
catch (InvalidOperationException)
{
    // AddNew in progress; retry after CommitNew/CancelNew
}

Prevention

When it happens

Trigger: Setting NewItemPlaceholderPosition (to e.g. First or Last) after calling AddNew() but before CommitNew()/CancelNew(); also deferred via DeferAction if raised during item removal.

Common situations: Toggling placeholder placement in UI code (e.g. moving the 'add new' placeholder to the top of a DataGrid) while a new-item row is already 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/2195552fb6b7ab1c. Report an issue: GitHub.

Appendix: source

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

        #endregion Public Properties

        #region IEditableCollectionView

        #region Adding new items

        /// <summary>
        /// Indicates whether to include a placeholder for a new item, and if so,
        /// where to put it.
        /// </summary>
        public NewItemPlaceholderPosition NewItemPlaceholderPosition
        {
            get { return _newItemPlaceholderPosition; }
            set
            {
                VerifyRefreshNotDeferred();

                if (value != _newItemPlaceholderPosition && IsAddingNew)
                    throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, "NewItemPlaceholderPosition", "AddNew"));

                if (value != _newItemPlaceholderPosition && _isRemoving)
                {
                    DeferAction(() => { NewItemPlaceholderPosition = value; });
                    return;
                }

                NotifyCollectionChangedEventArgs args = null;
                int oldIndex=-1, newIndex=-1;

                // we're adding, removing, or moving the placeholder.
                // Determine the appropriate events.
                switch (value)
                {
                    case NewItemPlaceholderPosition.None:
                        switch (_newItemPlaceholderPosition)
                        {
                            case NewItemPlaceholderPosition.None:

View on GitHub (pinned to 81131a70a4)