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

Fix submenu navigation in NavigationViewItem #759

Closed

Conversation

koal44
Copy link
Contributor

@koal44 koal44 commented Sep 12, 2023

The feature for submenus in NavigationViewTop was previously implemented but left unused prolly because of odd behavior such as stack overflow errors in AutoSuggest.

The problem was traced to the NavigationViewItem.MenuItems dependency property, where a new ObservableCollection was incorrectly used as the default value. According to MS Docs, "dependency property metadata shouldn't include a default reference-type value because that value will be assigned to all instances of the class, creating a singleton class."

Pull request type

Please check the type of change your PR introduces:

  • Update
  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes

What is the current behavior?

In the Gallery app if you add submenu items to NavigationView.Top you will get stack overflow
errors on AddItemsToAutoSuggestBoxItems(IList list) for menuItems.

<ui:NavigationView PaneDisplayMode="Top">
ui:NavigationView.MenuItems
<ui:NavigationViewItem Content="Menu Item 1"
ui:NavigationViewItem.MenuItems
<ui:NavigationViewItem Content="Menu SubItem 1" />
<ui:NavigationViewItem Content="Menu SubItem 2" />
</ui:NavigationViewItem.MenuItems>
</ui:NavigationViewItem>

Issue Number: N/A

What is the new behavior?

  • Change MenuItems to an ObservableCollection and initialize in ctor
  • Fix related properties such as HasMenuItems and MenuItemsSource (removed)
  • Add demo in the Gallery app
  • Other information

    image

The feature for submenus in NavigationViewTop was previously implemented
but left unused prolly because of odd behavior such as stack overflow errors in
AutoSuggest.

The problem was traced to the NavigationViewItem.MenuItems dependency
property, where a new ObservableCollection was incorrectly used as the default
value. According to [MS Docs], "dependency property metadata
shouldn't include a default reference-type value because that value will
be assigned to all instances of the class, creating a singleton class."
@koal44 koal44 requested a review from pomianowski as a code owner September 12, 2023 21:43
@syntax-tm
Copy link
Contributor

Why did you remove the MenuItemsSource?

Items is for defining in XAML. This is usually an object[] but it contains FrameworkElements.
ItemsSource is for databinding to a collection. Typically uses a DataTemplate to display the items.

If one of those was broken, then it's better to fix it instead of removing one of the options. You should be able to define a menu directly in XAML or bind to an IEnumerable source (with support for INotifyCollectionChanged).

@koal44
Copy link
Contributor Author

koal44 commented Sep 13, 2023

Ah, thx for the feedback. My intent wasn't to limit options but to keep MenuItems to a single source of truth. I agree that NavigationViewItem is fundamentally an ItemsControl, and retaining MenuItemsSource makes sense. To test compatibility with view models, I made an experimental branch here and basically ran smack into a bug where binding to MenuItems nullified the collection. I'll keep working on it in the morning.

@koal44
Copy link
Contributor Author

koal44 commented Sep 13, 2023

Here's an update on MenuItems working with both xaml and viewmodels. I'm still trying to understand why MS uses a source property for ItemsControl; it seems unneeded here. But i prolly lack the experience to make that call.

image

@pomianowski pomianowski self-assigned this Sep 16, 2023
@pomianowski pomianowski added enhancement New feature or request controls Changes to the appearance or logic of custom controls. MVVM_DI Issues related to MVVM and Dependency Injection. navigation Changes to navigation related controls. labels Sep 16, 2023
@github-actions github-actions bot removed the controls Changes to the appearance or logic of custom controls. label Sep 16, 2023
koal44 and others added 5 commits September 16, 2023 19:28
The feature for submenus in NavigationViewTop was previously implemented
but left unused prolly because of odd behavior such as stack overflow errors in
AutoSuggest.

The problem was traced to the NavigationViewItem.MenuItems dependency
property, where a new ObservableCollection was incorrectly used as the default
value. According to [MS Docs], "dependency property metadata
shouldn't include a default reference-type value because that value will
be assigned to all instances of the class, creating a singleton class."
@github-actions github-actions bot added the PR Pull request label Sep 20, 2023
@koal44 koal44 closed this Sep 20, 2023
@koal44 koal44 deleted the feature-add-submenu-navigation branch September 20, 2023 22:42
@koal44 koal44 mentioned this pull request Mar 23, 2024
7 tasks
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request MVVM_DI Issues related to MVVM and Dependency Injection. navigation Changes to navigation related controls. PR Pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants