如何解决UWP:绑定和添加新项目时出现问题
所以我使用的是uwp数据网格控件,这个是https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/datagrid
我的网格的ItemsSource绑定到一个ObservableCollection。用户可以单击添加按钮,将新项插入到集合中:AllPlans.Insert(0,newLessonPlan);
起初效果很好,直到用户单击更改集合的切换控件(切换事件)为止
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;
}
}
然后,当用户尝试向集合中添加另一个项目时,它会落在AllPlans.Insert(0,newLessonPlan);
处,这就是说索引0超出范围。
堆栈跟踪:
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 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
如果我在切换事件中将PlansGrid.ItemsSource = AllPlans;
注释掉,则不会收到该错误,但是用户不会在网格上看到该集合中的项目。
使用AllPlans.Add(newLessonPlan);
是可行的,但是我尝试使用PlansGrid.ScrollIntoView(newLessonPlan,null);
,它使System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
的作用与先前所述的相同。
有人有这样的东西吗?
我创建了一个显示错误的示例项目:https://1drv.ms/u/s!Avf9IdqZIPdXi2GAxF5meYl7oxIv?e=CnJe4N
进一步检查时,似乎是用户界面以及在网格中使用RowDetailsTemplate时出现问题。
在示例项目中,如果单击“ Fill Long”按钮,它将更改集合,因此网格具有可以滚动的足够数据,然后在单击“添加行”按钮时发生错误。
如果单击“ Fill Short”按钮,则只有两行,并且由于没有足够的数据而无法滚动网格,因此当您单击“添加一行”时不会发生错误,但是如果您再次单击它将会发生,因为网格现在有了滚动条,因为现在有更多数据。
如果单击“填充短”按钮,然后调整窗口的大小以使其高度减小,则当您单击“添加一行”按钮时,也会发生错误。
解决方法
此问题似乎是异常渲染,是由StringBuilder
引起的。
作为解决方法,您可以编写以下代码:
RowDetailsTemplate
在插入数据之前,首先将private void AddOne_Click(object sender,RoutedEventArgs e)
{
var randomGenerator = new Random();
var entitity1 = new TestEntity();
entitity1.Name = randomGenerator.Next(10,999999).ToString();
entitity1.Description = "New Random";
TestGrid.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;
CollectionOfEntities.Insert(0,entitity1);
TestGrid.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Visible;
}
设置为“已折叠”,然后再恢复,这样就不会报告任何错误。
您也可以在Github上报告此问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。