diff --git a/src/MvvmScarletToolkit.Incubator/MvvmScarletToolkit.Incubator.csproj b/src/MvvmScarletToolkit.Incubator/MvvmScarletToolkit.Incubator.csproj index 231e3448..dc8a53a0 100644 --- a/src/MvvmScarletToolkit.Incubator/MvvmScarletToolkit.Incubator.csproj +++ b/src/MvvmScarletToolkit.Incubator/MvvmScarletToolkit.Incubator.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/MvvmScarletToolkit.Wpf/Behaviors/MultiSelectionBehavior.cs b/src/MvvmScarletToolkit.Wpf/Behaviors/MultiSelectionBehavior.cs index 6e4073c4..0b85ee7d 100644 --- a/src/MvvmScarletToolkit.Wpf/Behaviors/MultiSelectionBehavior.cs +++ b/src/MvvmScarletToolkit.Wpf/Behaviors/MultiSelectionBehavior.cs @@ -44,7 +44,7 @@ protected override void OnAttached() { base.OnAttached(); - AssociatedObject.SelectionChanged += ListBoxSelectionChanged; + AssociatedObject.SelectionChanged += OnSelectionChanged; if (SelectedItems != null) { @@ -62,13 +62,13 @@ protected override void OnAttached() protected override void OnDetaching() { - AssociatedObject.SelectionChanged -= ListBoxSelectionChanged; + AssociatedObject.SelectionChanged -= OnSelectionChanged; base.OnDetaching(); } private static void OnSelectedItemsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { - if (!(o is MultiSelectionBehavior behavior)) + if (o is not MultiSelectionBehavior behavior) { return; } @@ -85,7 +85,7 @@ private static void OnSelectedItemsChanged(DependencyObject o, DependencyPropert if (e.NewValue is INotifyCollectionChanged newValue) { - if (e.OldValue != null) // skip setting the initial value from the UI(since thats an empty collection), as that will overwrite anything that has been set in the bound object + if (e.OldValue is not null) // skip setting the initial value from the UI(since thats an empty collection), as that will overwrite anything that has been set in the bound object { if (behavior.AssociatedObject.SelectedItems.Count > 0) { @@ -116,7 +116,7 @@ private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEven { _isUpdatingTarget = true; - if (e.OldItems != null) + if (e.OldItems is not null) { foreach (var item in e.OldItems) { @@ -124,7 +124,7 @@ private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEven } } - if (e.NewItems != null) + if (e.NewItems is not null) { foreach (var item in e.NewItems) { @@ -143,13 +143,18 @@ private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEven } } - private void ListBoxSelectionChanged(object? sender, SelectionChangedEventArgs e) + private void OnSelectionChanged(object? sender, SelectionChangedEventArgs e) { if (_isUpdatingTarget) { return; } + if (!ReferenceEquals(e.OriginalSource, sender)) + { + return; + } + var selectedItems = SelectedItems; if (selectedItems is null) {