Skip to content

Fixed menu item add when item added to the ObservableCollection #1092

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Wpf.Ui.Gallery/Views/Windows/SandboxWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SandboxWindow(SandboxWindowViewModel viewModel)

if (configurationBasedLogic)
{
_ = MyTestNavigationView.MenuItems.Add(
MyTestNavigationView.MenuItems.Add(
new NavigationViewItem("Test", SymbolRegular.Home24, typeof(SamplePage2))
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Wpf.Ui/Controls/NavigationView/INavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Based on Windows UI Library
// Copyright(c) Microsoft Corporation.All rights reserved.

using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using Wpf.Ui.Animations;

Expand Down Expand Up @@ -36,22 +36,22 @@ public interface INavigationView
/// <summary>
/// Gets the collection of menu items displayed in the NavigationView.
/// </summary>
IList MenuItems { get; }
ObservableCollection<object> MenuItems { get; }

/// <summary>
/// Gets or sets an object source used to generate the content of the NavigationView menu.
/// </summary>
object? MenuItemsSource { get; set; }
ObservableCollection<object> MenuItemsSource { get; set; }

/// <summary>
/// Gets the list of objects to be used as navigation items in the footer menu.
/// </summary>
IList FooterMenuItems { get; }
ObservableCollection<object> FooterMenuItems { get; }

/// <summary>
/// Gets or sets the object that represents the navigation items to be used in the footer menu.
/// </summary>
object? FooterMenuItemsSource { get; set; }
ObservableCollection<object> FooterMenuItemsSource { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
I seems they are suggesting to use ObservableCollection. According to the documentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a recommendation to the end user, which does not mean that the library should enforce this type of collection


/// <summary>
/// Gets the selected item.
Expand Down
2 changes: 1 addition & 1 deletion src/Wpf.Ui/Controls/NavigationView/NavigationView.Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public NavigationView()
SetValue(MenuItemsPropertyKey, menuItems);

var footerMenuItems = new ObservableCollection<object>();
footerMenuItems.CollectionChanged += OnMenuItems_CollectionChanged;
footerMenuItems.CollectionChanged += OnFooterMenuItems_CollectionChanged;
SetValue(FooterMenuItemsPropertyKey, footerMenuItems);
}

Expand Down
61 changes: 29 additions & 32 deletions src/Wpf.Ui/Controls/NavigationView/NavigationView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Controls;
Expand Down Expand Up @@ -62,7 +61,7 @@ public partial class NavigationView
/// <summary>Identifies the <see cref="MenuItemsSource"/> dependency property.</summary>
public static readonly DependencyProperty MenuItemsSourceProperty = DependencyProperty.Register(
nameof(MenuItemsSource),
typeof(object),
typeof(ObservableCollection<object>),
typeof(NavigationView),
new FrameworkPropertyMetadata(null, OnMenuItemsSourceChanged)
);
Expand All @@ -82,7 +81,7 @@ public partial class NavigationView
/// <summary>Identifies the <see cref="FooterMenuItemsSource"/> dependency property.</summary>
public static readonly DependencyProperty FooterMenuItemsSourceProperty = DependencyProperty.Register(
nameof(FooterMenuItemsSource),
typeof(object),
typeof(ObservableCollection<object>),
typeof(NavigationView),
new FrameworkPropertyMetadata(null, OnFooterMenuItemsSourceChanged)
);
Expand Down Expand Up @@ -274,13 +273,13 @@ public bool AlwaysShowHeader
}

/// <inheritdoc/>
public IList MenuItems => (ObservableCollection<object>)GetValue(MenuItemsProperty);
public ObservableCollection<object> MenuItems => (ObservableCollection<object>)GetValue(MenuItemsProperty);

/// <inheritdoc/>
[Bindable(true)]
public object? MenuItemsSource
public ObservableCollection<object>? MenuItemsSource
{
get => GetValue(MenuItemsSourceProperty);
get => (ObservableCollection<object>?)GetValue(MenuItemsSourceProperty);
set
{
if (value is null)
Expand All @@ -295,13 +294,13 @@ public object? MenuItemsSource
}

/// <inheritdoc/>
public IList FooterMenuItems => (ObservableCollection<object>)GetValue(FooterMenuItemsProperty);
public ObservableCollection<object> FooterMenuItems => (ObservableCollection<object>)GetValue(FooterMenuItemsProperty);

/// <inheritdoc/>
[Bindable(true)]
public object? FooterMenuItemsSource
public ObservableCollection<object> FooterMenuItemsSource
{
get => GetValue(FooterMenuItemsSourceProperty);
get => (ObservableCollection<object>)GetValue(FooterMenuItemsSourceProperty);
set
{
if (value is null)
Expand Down Expand Up @@ -452,13 +451,11 @@ public Thickness FrameMargin

private void OnMenuItems_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems is null)
if (e.NewItems is not null)
{
return;
UpdateMenuItemsTemplate(e.NewItems);
AddItemsToDictionaries(e.NewItems);
}

UpdateMenuItemsTemplate(e.NewItems);
AddItemsToDictionaries(e.NewItems);
}

private static void OnMenuItemsSourceChanged(DependencyObject? d, DependencyPropertyChangedEventArgs e)
Expand All @@ -468,18 +465,18 @@ private static void OnMenuItemsSourceChanged(DependencyObject? d, DependencyProp
return;
}

navigationView.MenuItems.Clear();

if (e.NewValue is IEnumerable newItemsSource and not string)
if (navigationView is not null)
{
foreach (var item in newItemsSource)
{
navigationView.MenuItems.Add(item);
}
navigationView.OnMenuItemsSourceChanged(e);
}
else if (e.NewValue != null)
}

private void OnMenuItemsSourceChanged(DependencyPropertyChangedEventArgs e)
{
if (MenuItemsSource is not null)
{
navigationView.MenuItems.Add(e.NewValue);
MenuItemsSource.CollectionChanged += OnMenuItems_CollectionChanged;
SetValue(MenuItemsPropertyKey, MenuItemsSource);
}
}

Expand All @@ -504,18 +501,18 @@ DependencyPropertyChangedEventArgs e
return;
}

navigationView.FooterMenuItems.Clear();

if (e.NewValue is IEnumerable newItemsSource and not string)
if (navigationView is not null)
{
foreach (var item in newItemsSource)
{
navigationView.FooterMenuItems.Add(item);
}
navigationView.OnFooterMenuItemsSourceChanged(e);
}
else if (e.NewValue != null)
}

private void OnFooterMenuItemsSourceChanged(DependencyPropertyChangedEventArgs e)
{
if (FooterMenuItemsSource is not null)
{
navigationView.FooterMenuItems.Add(e.NewValue);
FooterMenuItemsSource.CollectionChanged += OnFooterMenuItems_CollectionChanged;
SetValue(FooterMenuItemsPropertyKey, FooterMenuItemsSource);
}
}

Expand Down