Skip to content

Problem when selecting the previously clicked entry, if the object is a child. #6

@JK-MS1

Description

@JK-MS1

the code fails to find the tag if the NavigationItem is a MenuItems child object, so I had to modify and add a function to recursively search for it.

Sorry I can't push the change, because I'm not sure of myself on github...

MainWindow.xaml.cs

`

    private NavigationViewItem FindNavigationViewItemByTag(IEnumerable<object> menuItems, string tag)
    {
        foreach (var item in menuItems)
        {
            if (item is NavigationViewItem navigationViewItem)
            {
                if (navigationViewItem.Tag?.ToString() == tag)
                {
                    return navigationViewItem;
                }

                var foundItem = FindNavigationViewItemByTag(navigationViewItem.MenuItems, tag);
                if (foundItem != null)
                {
                    return foundItem;
                }
            }
        }
        return null;
    }


 private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
 {
     NavigationViewControl.IsBackEnabled = ContentFrame.CanGoBack;

     if (ContentFrame.SourcePageType == typeof(Views.SettingsPage))
     {
         // SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag.
         NavigationViewControl.SelectedItem = (NavigationViewItem)NavigationViewControl.SettingsItem;
     }
     else if (ContentFrame.SourcePageType != null)
     {
         //  NavigationViewControl.SelectedItem = NavigationViewControl.MenuItems
         //      .OfType<NavigationViewItem>()
         //      .First(n => n.Tag.Equals(ContentFrame.SourcePageType.FullName.ToString()));

         var selectedItem = FindNavigationViewItemByTag(NavigationViewControl.MenuItems, ContentFrame.SourcePageType.FullName.ToString());
         NavigationViewControl.SelectedItem = selectedItem;


     }

     NavigationViewControl.Header = ((NavigationViewItem)NavigationViewControl.SelectedItem)?.Content?.ToString();
 }

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions