Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearing Bound ObservableCollection causes System.InvalidOperationException: 'Added item does not appear at given index '0'.' #2267

Open
keelerjr12 opened this issue Dec 1, 2019 · 2 comments

Comments

@keelerjr12
Copy link

@keelerjr12 keelerjr12 commented Dec 1, 2019

When calling ObservableCollection.Clear() and then re-loading (i.e. adding) items back into ObservableCollection, a System.InvalidOperationException: 'Added item does not appear at given index '0'.' gets thrown. I have provided a snippet of my code below. I have indicated where the issue is.

        private void TransactionCreated(object? sender, EventArgs e)
        {
            if (!(sender is TransactionRowViewModel row))
                return;

            if (row.Account == null)
                    return;

            var id = _transactionService.Create(row.Date, row.Account.Id, _accountId, row.Value, row.Description);
            row.TransactionId = id;

            //Transactions = new ObservableCollection<TransactionRowViewModel>();
            Transactions.Clear();
            LoadTransactions(); 

            // ISSUE: After TransactionCreated returns, exception is thrown!
        }

And here is the stack trace:

Unhandled exception. System.InvalidOperationException: Added item does not appear at given index '0'.
   at System.Windows.Data.ListCollectionView.AdjustBefore(NotifyCollectionChangedAction action, Object item, Int32 index)
   at System.Windows.Data.ListCollectionView.CommitNew()
   at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.CommitNew()
   at System.Windows.Controls.DataGrid.CommitRowItem()
   at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
   at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(Object sender, ExecutedRoutedEventArgs e)
   at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
   at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
   at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
   at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
   at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode)
   at System.Windows.Controls.DataGrid.CommitAnyEdit()
   at System.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e)
   at System.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e)
   at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
   at System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
   at System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
   at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
   at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
@weltkante

This comment has been minimized.

Copy link

@weltkante weltkante commented Dec 2, 2019

I'm not part of the WPF team but you'll probably have to add more context (ideally a piece of code which can be used to reproduce the scenario). The stack trace indicates that WPF is processing keyboard input when things go wrong and your issue description contains no information of what you were doing to trigger this or how TransactionCreated is called. I doubt its as simple as you describe it, if it is the call stack doesn't fit your described scenario.

@arpitmathur

This comment has been minimized.

Copy link
Contributor

@arpitmathur arpitmathur commented Dec 3, 2019

@keelerjr12 Can you please provide a sample app that reproduces the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.