Hi all,
So I am using a uwp datagrid control, this one: https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/datagrid
The ItemsSource of my grid is bound to an ObservableCollection. The user can click on an add button which inserts a new item to the collection: AllPlans.Insert(0, newLessonPlan);
It works well and good at first, until the user clicks on a toggle control which changes the collection, the toggle event:
private void ToggleOldEvents_Toggled(object sender, RoutedEventArgs e)
{
AllPlans = new ObservableCollection<Entities.LessonPlan>(PlanController.GetAll(((Entities.AppUser)UserBox.SelectedItem).id, ToggleOldEvents.IsOn).OrderByDescending(p => p.DateTimeFrom));
PlansGrid.ItemsSource = AllPlans;
foreach (var col in PlansGrid.Columns)
{
col.SortDirection = null;
}
}
Then when the user tries to add another item to the collection, it falls over at AllPlans.Insert(0, newLessonPlan);
It is saying that the index 0 is out of bounds.
Stack trace:
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
at System.Collections.Generic.List`1.RemoveAt(Int32 index)
at Microsoft.Toolkit.Uwp.UI.Controls.DataGridInternals.DataGridDisplayData.UnloadScrollingElement(Int32 slot, Boolean updateSlotInformation, Boolean wasDeleted)
at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.RemoveNonDisplayedRows(Int32 newFirstDisplayedSlot, Int32 newLastDisplayedSlot)
at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.UpdateDisplayedRows(Int32 newFirstDisplayedSlot, Double displayHeight)
at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.ComputeScrollBarsLayout()
at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.OnAddedElement_Phase2(Int32 slot, Boolean updateVerticalScrollBarOnly)
at Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.InsertRowAt(Int32 rowIndex)
at Microsoft.Toolkit.Uwp.UI.Data.Utilities.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
at Microsoft.Toolkit.Uwp.UI.Data.Utilities.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(EffectiveNotifyCollectionChangedAction action, Object oldItem, Object newItem, Int32 adjustedOldIndex, Int32 adjustedNewIndex)
at Microsoft.Toolkit.Uwp.UI.Data.Utilities.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32 adjustedOldIndex, Int32 adjustedNewIndex)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at TeachItUWP.Pages.LessonPlanningPage.AddEvent_Click(Object sender, RoutedEventArgs e) in C:\Users\pavel\source\repos\TeachItUWP\TeachItUWP\Pages\LessonPlanningPage.xaml.cs:line 270
If I comment out the PlansGrid.ItemsSource = AllPlans;
in my toggle event, I don't get the error, but then the user doesn't see the items in the collection on the grid.
Using AllPlans.Add(newLessonPlan);
works, but I was trying to use PlansGrid.ScrollIntoView(newLessonPlan, null);
which gives me System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
after the same user behaviour described earlier.
Anyone had something like this?