From c66ee137feb563521125c055514d1ea24636ba72 Mon Sep 17 00:00:00 2001 From: Waheed Ahmad Date: Mon, 1 Nov 2021 09:46:49 +0500 Subject: [PATCH 1/3] Add Test_AdvancedCollectionView_Using_with_DataGrid --- .../UI/Test_AdvancedCollectionView.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/UnitTests/UnitTests.UWP/UI/Test_AdvancedCollectionView.cs b/UnitTests/UnitTests.UWP/UI/Test_AdvancedCollectionView.cs index 9a5f59fe457..a323d9536d7 100644 --- a/UnitTests/UnitTests.UWP/UI/Test_AdvancedCollectionView.cs +++ b/UnitTests/UnitTests.UWP/UI/Test_AdvancedCollectionView.cs @@ -8,6 +8,7 @@ using System.Collections.ObjectModel; using System.Linq; using Microsoft.Toolkit.Uwp.UI; +using Microsoft.Toolkit.Uwp.UI.Controls; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer; @@ -1053,6 +1054,27 @@ public void Test_AdvancedCollectionView_Sorting_CustomComparable_With_Shaping() Assert.AreEqual(42, ((Person)a.First()).Age); } + [TestCategory("AdvancedCollectionView")] + [UITestMethod] + public void Test_AdvancedCollectionView_Using_with_DataGrid() + { + var l = new ObservableCollection + { + "lorem", + "ipsum", + "dolor", + "sit", + "amet" + }; + + var a = new AdvancedCollectionView { Source = l }; + new DataGrid { ItemsSource = a }; + Assert.AreEqual(5, a.Count); + + l.RemoveAt(l.Count - 1); + Assert.AreEqual(4, a.Count); + } + private class DelegateComparable : IComparer { private Func _func; From a067b9287ddb180e91b509e63b9bd9ebd5950490 Mon Sep 17 00:00:00 2001 From: Waheed Ahmad Date: Mon, 1 Nov 2021 09:49:32 +0500 Subject: [PATCH 2/3] Fixes IObservableVector based ItemsSource exception --- .../DataGrid/DataGridDataConnection.cs | 2 +- .../DataGrid/DataGridRows.cs | 7 +++++++ UnitTests/UnitTests.UWP/UnitTests.UWP.csproj | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridDataConnection.cs b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridDataConnection.cs index 1b5661bd0d0..a0ed619fbbe 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridDataConnection.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridDataConnection.cs @@ -954,7 +954,7 @@ private void NotifyingDataSource_VectorChanged(IObservableVector sender, { // If we're grouping then we handle this through the CollectionViewGroup notifications. // Remove is a single item operation. - _owner.RemoveRowAt(index, sender[index]); + _owner.RemoveRowAt(index); } break; diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs index 7146e112b26..54580612be7 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs @@ -605,6 +605,13 @@ internal void RefreshRows(bool recycleRows, bool clearRows) } } + internal void RemoveRowAt(int rowIndex) + { + IEnumerable rows = GetAllRows(); + object item = rows.ElementAtOrDefault(rowIndex)?.DataContext; + RemoveElementAt(SlotFromRowIndex(rowIndex), item, true); + } + internal void RemoveRowAt(int rowIndex, object item) { RemoveElementAt(SlotFromRowIndex(rowIndex), item, true); diff --git a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj index cdbb101c62a..95a345c6937 100644 --- a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj +++ b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj @@ -306,6 +306,10 @@ {d4ff799d-0df2-495a-adc9-3bbc4aef8971} Microsoft.Toolkit.Uwp.UI.Behaviors + + {DAEB9CEC-C817-33B2-74B2-BC379380DB72} + Microsoft.Toolkit.Uwp.UI.Controls.DataGrid + {cb444381-18ba-4a51-bb32-3a498bcc1e99} Microsoft.Toolkit.Uwp.UI.Controls.Layout From cc4992e8eb625cad0ff1fe8023dbf5b75a83ddac Mon Sep 17 00:00:00 2001 From: Waheed Ahmad Date: Wed, 3 Nov 2021 02:55:13 +0500 Subject: [PATCH 3/3] Added GetRow() method to get single row at given index --- .../DataGrid/DataGridRows.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs index 54580612be7..d94d31d2c3b 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridRows.cs @@ -607,8 +607,7 @@ internal void RefreshRows(bool recycleRows, bool clearRows) internal void RemoveRowAt(int rowIndex) { - IEnumerable rows = GetAllRows(); - object item = rows.ElementAtOrDefault(rowIndex)?.DataContext; + object item = GetRow(rowIndex)?.DataContext; RemoveElementAt(SlotFromRowIndex(rowIndex), item, true); } @@ -1382,6 +1381,18 @@ private void EnsureRowDetailsVisibility( raiseNotification); } + private DataGridRow GetRow(int index) + { + if (_rowsPresenter != null) + { + return _rowsPresenter.Children[index] as DataGridRow; + } + else + { + return null; + } + } + private IEnumerable GetAllRows() { if (_rowsPresenter != null)