【问题标题】:UWP: Problem with binding and adding new itemsUWP:绑定和添加新项目的问题
【发布时间】:2020-08-26 07:49:27
【问题描述】:

所以我用的是uwp datagrid控件,这个: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 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

如果我在切换事件中注释掉 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”按钮,它将更改集合,使网格有足够的数据可以滚动,然后当您单击“Add a Row”按钮时会发生错误。

如果单击“填充短”按钮,因此只有 2 行,并且由于数据不足而无法滚动网格,则单击“添加行”时不会发生错误,但如果您再次单击它会发生,因为网格现在有一个滚动条,因为它现在有更多数据。

如果您单击“填充短”按钮,然后调整窗口大小使其高度变小,当您单击“添加行”按钮时,也会发生错误。

【问题讨论】:

  • 您好,我尝试创建一个DataGrid,并进行了更改数据源和向新数据源添加数据的操作,没有异常。那么能否提供一个最小可运行的demo,以便我们分析问题的具体原因?
  • 您好,我创建了一个显示错误的示例项目:1drv.ms/u/s!Avf9IdqZIPdXi2GAxF5meYl7oxIv?e=CnJe4N 经过进一步检查,这似乎是用户界面的问题,并且当您在网格中使用 RowDetailsTemplate 时。我编辑了我的问题以提供更多详细信息。

标签: uwp


【解决方案1】:

这个问题貌似是渲染异常,是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;
}

插入数据前,先将RowDetailsVisibilityMode设置为Collapsed,然后恢复,这样就不会报错了。

您也可以在Github 上报告此问题。

【讨论】:

  • 嗨,是的,我通过在插入之前将网格 ItemSource 设置为 null,然后将其设置回插入之后的状态,找到了类似的解决方法。我没有 github 帐户,所以请随意窃取此发现报告! ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多