From 5725019bd5fb3f89158e3faa4994b8daeb87c7f1 Mon Sep 17 00:00:00 2001 From: Nzroops Date: Tue, 20 Feb 2024 12:39:30 +0800 Subject: [PATCH 1/2] Update NavigationView.Properties.cs UpdateMenuItemsTemplate And AddItemsToDictionaries when MenuItems changed. --- .../NavigationView.Properties.cs | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs b/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs index 592ef644c..5a500562c 100644 --- a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs +++ b/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs @@ -7,6 +7,7 @@ // Copyright(c) Microsoft Corporation.All rights reserved. using System.Collections; +using System.Collections.Specialized; using System.Windows.Controls; using Wpf.Ui.Animations; @@ -82,7 +83,7 @@ public partial class NavigationView nameof(FooterMenuItemsProperty), typeof(IList), typeof(NavigationView), - new FrameworkPropertyMetadata(null) + new FrameworkPropertyMetadata(null, OnFooterMenuItemsPropertyChanged) ); /// @@ -513,16 +514,40 @@ private static void OnMenuItemsPropertyChanged(DependencyObject? d, DependencyPr if (navigationView.MenuItemsItemsControl is null) { + navigationView.UpdateCollectionChangedEvent(e.OldValue as IList, e.NewValue as IList); return; } if (navigationView.MenuItemsItemsControl.ItemsSource.Equals(enumerableNewValue)) { + navigationView.UpdateMenuItemsTemplate(enumerableNewValue); return; } navigationView.MenuItemsItemsControl.ItemsSource = null; navigationView.MenuItemsItemsControl.ItemsSource = enumerableNewValue; + navigationView.UpdateMenuItemsTemplate(enumerableNewValue); + navigationView.AddItemsToDictionaries(enumerableNewValue); + + navigationView.UpdateCollectionChangedEvent(e.OldValue as IList, e.NewValue as IList); + } + + private void UpdateCollectionChangedEvent(IList? oldMenuItems, IList? newMenuItems) + { + if(oldMenuItems is INotifyCollectionChanged notifyCollection) + { + notifyCollection.CollectionChanged -= OnMenuItems_CollectionChanged; + } + if(newMenuItems is INotifyCollectionChanged newNotifyCollection) + { + newNotifyCollection.CollectionChanged += OnMenuItems_CollectionChanged; + } + } + + private void OnMenuItems_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + UpdateMenuItemsTemplate(e.NewItems); + AddItemsToDictionaries(e.NewItems); } private static void OnMenuItemsSourcePropertyChanged( @@ -551,6 +576,31 @@ DependencyPropertyChangedEventArgs e navigationView.FooterMenuItems = enumerableNewValue; } + private static void OnFooterMenuItemsPropertyChanged(DependencyObject? d, DependencyPropertyChangedEventArgs e) + { + if (d is not NavigationView navigationView || e.NewValue is not IList enumerableNewValue) + { + return; + } + + if (navigationView.FooterMenuItemsItemsControl is null) + { + navigationView.UpdateCollectionChangedEvent(e.OldValue as IList, e.NewValue as IList); + return; + } + + if (navigationView.FooterMenuItemsItemsControl.ItemsSource.Equals(enumerableNewValue)) + { + return; + } + + navigationView.FooterMenuItemsItemsControl.ItemsSource = null; + navigationView.FooterMenuItemsItemsControl.ItemsSource = enumerableNewValue; + navigationView.UpdateMenuItemsTemplate(enumerableNewValue); + navigationView.AddItemsToDictionaries(enumerableNewValue); + navigationView.UpdateCollectionChangedEvent(e.OldValue as IList, e.NewValue as IList); + } + private static void OnPaneDisplayModePropertyChanged( DependencyObject? d, DependencyPropertyChangedEventArgs e From 43af1005b1c4d960cdf9a2257022c058b9dcccce Mon Sep 17 00:00:00 2001 From: Nzroops Date: Tue, 20 Feb 2024 13:58:02 +0800 Subject: [PATCH 2/2] add null check befor UpdateMenuItemsTemplate/AddItemsToDictionaries when MenuItems_CollectionChanged #964 --- .../Controls/NavigationView/NavigationView.Properties.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs b/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs index 5a500562c..4c5ecd779 100644 --- a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs +++ b/src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs @@ -546,6 +546,11 @@ private void UpdateCollectionChangedEvent(IList? oldMenuItems, IList? newMenuIte private void OnMenuItems_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { + if (e.NewItems is null) + { + return; + } + UpdateMenuItemsTemplate(e.NewItems); AddItemsToDictionaries(e.NewItems); }