Need to trigger a NavigationView.SelectionChanged event in code behind

David Hoffman 121 Reputation points
2021-10-21T14:53:55.24+00:00

I have a need to trigger a NavigationView.SelectionChanged event in code (C++) to take the user to a specific page that I already done in XAml. The Navigation for my app works just fine. I have several pages available on a Top nav bar, including Settings. Here is a pic.
142524-mainnavmenu.png

Here is the view of Settings nav bar.
142455-settingnavmenu.png

During start up, I check a dB to see if Factory Settings are present. If not, I want to take the user to that page. This happens on the main Page where I am showing a modal message box that they need to set Factory settings.

I followed the example from MS of how to code a complete Navigation for my UWP app, what I don't know how to do is make the navigation event happen.

Here are the functions I implemented for both the Main Page and Settings page. I can show my code, but it is working. Remember this an event I am trying to fire in code.

    void ContentFrame_NavigationFailed(IInspectable const& sender, NavigationFailedEventArgs const& args);  
    void NavView_Loaded(IInspectable const& sender, RoutedEventArgs const& args);  
    void NavView_SelectionChanged(IInspectable const& sender, NavigationViewSelectionChangedEventArgs const& args);  
    void NavView_Navigate(wstring navItemTag, NavigationTransitionInfo const& transitionInfo);  
    void NavView_BackRequested(NavigationView const& sender, NavigationViewBackRequestedEventArgs const& args);  
    void CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher const& sender, AcceleratorKeyEventArgs const& args);  
    void CoreWindow_PointerPressed(CoreWindow const& sender, PointerEventArgs const& args);  
    void System_BackRequested(IInspectable const& sender, BackRequestedEventArgs const& args);  
    bool TryGoBack(void);  
    void On_Navigated(IInspectable const& sender, NavigationEventArgs const& args);  

Thank you for your help.

Universal Windows Platform (UWP)
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,111 Reputation points Microsoft Vendor
    2021-10-22T03:22:33.59+00:00

    Hello,

    Welcome to Microsoft Q&A!

    If I understand you correctly, what you want to do is to navigate to a page via code. If it is, what you need is to set the NavigationView.SelectedItem property. You could set the target NavigationViewItem to the NavigationView.SelectedItem property. Then the NavigationView.SelectionChanged event will be triggered and the navigation will happen.

    Like this:

      // Itme3 is a NavigationViewItem that I created in the Xaml  
     nvSample().SelectedItem(Item3());  
    

    If your NavigationViewItem is not created in Xaml, you might need to find it in NavigationView.MenuItems Property

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Samah Abdullah Abu El-Yazied 26 Reputation points
    2023-05-27T04:19:42.2+00:00

    Nice Topic

    0 comments No comments