Skip to content

Commit

Permalink
Merge pull request #190 from Insire/dev
Browse files Browse the repository at this point in the history
merge from dev
  • Loading branch information
Insire authored Nov 23, 2021
2 parents a777788 + 2bfcbdc commit 316912d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="DynamicData" Version="7.4.3" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.37" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>

Expand Down
19 changes: 12 additions & 7 deletions src/MvvmScarletToolkit.Wpf/Behaviors/MultiSelectionBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void OnAttached()
{
base.OnAttached();

AssociatedObject.SelectionChanged += ListBoxSelectionChanged;
AssociatedObject.SelectionChanged += OnSelectionChanged;

if (SelectedItems != null)
{
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -116,15 +116,15 @@ private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEven
{
_isUpdatingTarget = true;

if (e.OldItems != null)
if (e.OldItems is not null)
{
foreach (var item in e.OldItems)
{
AssociatedObject.SelectedItems.Remove(item);
}
}

if (e.NewItems != null)
if (e.NewItems is not null)
{
foreach (var item in e.NewItems)
{
Expand All @@ -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)
{
Expand Down

0 comments on commit 316912d

Please sign in to comment.