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() {