Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update INotifyCollectionChanged events in MenuItems and FooterMenuItems of the NavigationView #1098

Merged
merged 6 commits into from
Jun 11, 2024
53 changes: 53 additions & 0 deletions src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,49 @@
set => SetValue(FrameMarginProperty, value);
}

private void OnMenuItemsSource_CollectionChanged(object? sender, IList collection, NotifyCollectionChangedEventArgs e)
{
if (ReferenceEquals(sender, collection))
{
return;
}

switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var item in e.NewItems)

Check warning on line 463 in src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
pomianowski marked this conversation as resolved.
Show resolved Hide resolved
{
collection.Add(item);
}

Check warning on line 466 in src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs

View workflow job for this annotation

GitHub Actions / build

break;

case NotifyCollectionChangedAction.Remove:
foreach (var item in e.OldItems)

Check warning on line 470 in src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
if (!e.NewItems.Contains(item))

Check warning on line 472 in src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
collection.Remove(item);
}
}

Check warning on line 476 in src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs

View workflow job for this annotation

GitHub Actions / build

break;

case NotifyCollectionChangedAction.Move:
var moveItem = MenuItems[e.OldStartingIndex];
collection.RemoveAt(e.OldStartingIndex);
collection.Insert(e.NewStartingIndex, moveItem);
break;

case NotifyCollectionChangedAction.Replace:
collection.RemoveAt(e.OldStartingIndex);
collection.Insert(e.OldStartingIndex, e.NewItems[0]);

Check warning on line 487 in src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
break;

case NotifyCollectionChangedAction.Reset:
collection.Clear();
break;
}
}

private void OnMenuItems_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems is null)
Expand Down Expand Up @@ -481,6 +524,11 @@
{
navigationView.MenuItems.Add(e.NewValue);
}

if (e.NewValue is INotifyCollectionChanged oc)
{
oc.CollectionChanged += (s, e) => navigationView.OnMenuItemsSource_CollectionChanged(oc, navigationView.MenuItems, e);
}
}

private void OnFooterMenuItems_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
Expand Down Expand Up @@ -517,6 +565,11 @@
{
navigationView.FooterMenuItems.Add(e.NewValue);
}

if (e.NewValue is INotifyCollectionChanged oc)
{
oc.CollectionChanged += (s, e) => navigationView.OnMenuItemsSource_CollectionChanged(oc, navigationView.FooterMenuItems, e);
}
}

private static void OnPaneDisplayModeChanged(DependencyObject? d, DependencyPropertyChangedEventArgs e)
Expand Down
Loading