UWP NavigationView Selected Item as CommandParameter in MVVM Community Toolkit

DotNET Fan 211 Reputation points
2024-06-25T04:49:47.9033333+00:00

UWP/XAML experts , How to set the selecteditem tag value as the CommandParameter in NavigationView SelectionChanged? Using community MVVM toolkit here. In example below the tag value is 3456 and would change dynamically .

CommandParameter="{Binding ????????}"

<muxc:NavigationView x:Name="DocumentNavView" Grid.Row="3" Grid.Column="0"
                  PaneDisplayMode="Left" 
                  IsTabStop="False" IsSettingsVisible="False" OpenPaneLength="200" Margin="10" 
                 IsBackButtonVisible="Collapsed" >
     <interactivity:Interaction.Behaviors>
         <core:EventTriggerBehavior EventName="SelectionChanged">
             <core:InvokeCommandAction Command="{x:Bind ViewModel.DocumentChanged}" CommandParameter="{Binding }" />
           
         </core:EventTriggerBehavior>
     </interactivity:Interaction.Behaviors>
    
    
         <muxc:NavigationViewItem   ToolTipService.ToolTip="Document1" Tag="3456">
             <muxc:NavigationViewItem.Content>
                 <StackPanel Orientation="Horizontal" >
                     <Image Source="ms-appx:///Assets/Doc.png" Width="20" Height="20" Margin="0 0 8 0" />
                     <TextBlock Text="Document1" Margin="8 0 0 0"></TextBlock>
                 </StackPanel>
             </muxc:NavigationViewItem.Content>
         </muxc:NavigationViewItem>
        
     </muxc:NavigationView.MenuItems>
     <Frame x:Name="rootFrame" >
     <Frame.ContentTransitions>
         <TransitionCollection>
             <NavigationThemeTransition/>
         </TransitionCollection>
     </Frame.ContentTransitions>
     </Frame>
 </muxc:NavigationView>
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 18,481 Reputation points Microsoft Vendor
    2024-06-25T07:48:38.17+00:00

    Hi @DotNET Fan ,

    Welcome to Microsoft Q&A!

    It is recommended to bind the NavigationView, then you can get the item in ViewModel.

     <core:InvokeCommandAction Command="{x:Bind ViewModel.DocumentChanged}"  CommandParameter="{Binding ElementName=DocumentNavView}" />
    
    

     public class TestViewModel
     {
    
         public ICommand DocumentChanged
         {
             get
             {
                 return new CommadEventHandler<object>((s) => this.getContent(s));
             }
    
         }
    
         private void getContent(object obj)
         {
             try
             {
                 Microsoft.UI.Xaml.Controls.NavigationView navigationView = (Microsoft.UI.Xaml.Controls.NavigationView)obj;
                 Microsoft.UI.Xaml.Controls.NavigationViewItem item = navigationView.SelectedItem as Microsoft.UI.Xaml.Controls.NavigationViewItem;
                 Debug.WriteLine(item.Tag);
             }
             catch (Exception ex)
             {
                 Debug.WriteLine(ex.Message);
             }
         }
     }
    

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.