From b4c8750db3a085d1225c1645d53a3d9ab93cbbfa Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Mon, 25 Mar 2024 16:12:31 -0500 Subject: [PATCH] prevent leaks --- Maui.DataGrid/DataGrid.xaml.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Maui.DataGrid/DataGrid.xaml.cs b/Maui.DataGrid/DataGrid.xaml.cs index 29df551..64cfeb1 100644 --- a/Maui.DataGrid/DataGrid.xaml.cs +++ b/Maui.DataGrid/DataGrid.xaml.cs @@ -50,11 +50,6 @@ public DataGrid() { _collectionView.ItemsSource = InternalItems; } - - Loaded += (sender, e) => - { - Initialize(); - }; } #endregion ctor @@ -1226,6 +1221,16 @@ protected override void OnParentSet() { base.OnParentSet(); + if (Parent is null) + { + Loaded -= OnLoaded; + } + else + { + Loaded -= OnLoaded; + Loaded += OnLoaded; + } + if (Parent is null) { _collectionView.SelectionChanged -= OnSelectionChanged; @@ -1268,6 +1273,8 @@ protected override void OnParentSet() } } + private void OnLoaded(object? sender, EventArgs e) => Initialize(); + /// protected override void OnBindingContextChanged() {