diff --git a/src/Wpf.Ui/Controls/ListView/ListView.cs b/src/Wpf.Ui/Controls/ListView/ListView.cs index ef2b44a30..32e8862d3 100644 --- a/src/Wpf.Ui/Controls/ListView/ListView.cs +++ b/src/Wpf.Ui/Controls/ListView/ListView.cs @@ -26,6 +26,8 @@ namespace Wpf.Ui.Controls; /// public class ListView : System.Windows.Controls.ListView { + private DependencyPropertyDescriptor? _descriptor; + /// Identifies the dependency property. public static readonly DependencyProperty ViewStateProperty = DependencyProperty.Register( nameof(ViewState), @@ -62,6 +64,7 @@ protected virtual void OnViewStateChanged(DependencyPropertyChangedEventArgs e) public ListView() { Loaded += OnLoaded; + Unloaded += OnUnloaded; } private void OnLoaded(object sender, RoutedEventArgs e) @@ -69,14 +72,21 @@ private void OnLoaded(object sender, RoutedEventArgs e) Loaded -= OnLoaded; // prevent memory leaks // Setup initial ViewState and hook into View property changes - var descriptor = DependencyPropertyDescriptor.FromProperty( + _descriptor = DependencyPropertyDescriptor.FromProperty( System.Windows.Controls.ListView.ViewProperty, typeof(System.Windows.Controls.ListView) ); - descriptor?.AddValueChanged(this, OnViewPropertyChanged); + _descriptor?.AddValueChanged(this, OnViewPropertyChanged); UpdateViewState(); // set the initial state } + private void OnUnloaded(object sender, RoutedEventArgs e) + { + Unloaded -= OnUnloaded; + + _descriptor?.RemoveValueChanged(this, OnViewPropertyChanged); + } + private void OnViewPropertyChanged(object? sender, EventArgs e) { UpdateViewState();