I am building a C# WPF application with MahApps hamburger menu and right window commands (RWC) to navigate between different views. I am able to get the hamburger menu items to switch views associated with the Tag value of both items and option items. I am lost at how to switch the view when I click a RWC button.

I tried to add a hidden item to the hamburger menu items source corresponding to the new user control that should be loaded when RWC button is clicked. I am unable to change selected item programmatically as MVVM doesn't let me access controls from the view model.

What is the recommended approach for switching views using RWC buttons (XAML code below)? I skipped the data templates for hamburger menu and option items in the resource dictionary. The view model has commands with Execute methods for each item in the RWC menu, but I don't know how to change the hamburger menu selected item from those commands. The views that the RWC menu may or may not be associated with any hamburger menu item.

I understand that MVVM is meant to keep the view and logic separate, but it is difficult to manage the ContentControl's content in hamburger menu from outside.

<Controls:MetroWindow.Resources>

    <!-- Resource Dictionary: There should be only ONE resource dictionary in the resources node -->
    <ResourceDictionary>

        <vm:MainWindowViewModel x:Key="MainWindowVM" />
        <conv:SelectedHamburgerItemToViewConverter x:Key="selectedItemToContentConv" />

        <!-- Define the ContextMenu for top right window command buttons -->
        <ContextMenu x:Key="FileMenuContextMenu">
            <MenuItem Header="Create New File" FontSize="14" 
                      Command="{Binding ButtonCreateNewFileClick}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
            <MenuItem Header="Open Existing File" FontSize="14" 
                      Command="{Binding ButtonOpenExistingFileClick}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
            <MenuItem Header="Save as New File" FontSize="14" 
                      Command="{Binding ButtonSaveAsNewFileClick}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
            <MenuItem Header="Close Current File" FontSize="14" 
                      Command="{Binding ButtonCloseCurrentFileClick}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
        </ContextMenu>

        <ContextMenu x:Key="SettingsMenuContextMenu">
            <MenuItem Header="Program Settings" FontSize="14" 
                      Command="{Binding ButtonProgramSettingsClick}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
            <MenuItem Header="Custom Normative Values" FontSize="14" 
                      Command="{Binding ButtonCustomNormsClick}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}}" />
        </ContextMenu>

    </ResourceDictionary>
</Controls:MetroWindow.Resources>

<Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands>
        <Button x:Name="BtnFileMenuDropDown" Content="File Menu" 
                ContextMenu="{StaticResource FileMenuContextMenu}" 
                Command="{Binding ButtonFileMenuDropDownClick}" 
                CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
                ContextMenuService.HasDropShadow="True" ContextMenuService.Placement="Bottom">
            <Button.ContentTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="8 0">
                        <fa:ImageAwesome Icon="FolderOutline" Width="16" Foreground="White" Margin="0 0 6 0" />
                        <TextBlock Margin="4 0 0 0" VerticalAlignment="Center" 
                                   FontSize="14" Text="File Menu" />
                    </StackPanel>
                </DataTemplate>
            </Button.ContentTemplate>
        </Button>
        <Button x:Name="BtnSettingsMenuDropDown" Margin="8 0" 
                ContextMenu="{StaticResource SettingsMenuContextMenu}"  
                Command="{Binding ButtonSettingsMenuDropDownClick}" 
                CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
                ContextMenuService.HasDropShadow="True" ContextMenuService.Placement="Bottom">
            <Button.ContentTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="8 0">
                        <fa:ImageAwesome Icon="Gears" Width="16" Foreground="White" Margin="0 0 6 0" />
                        <TextBlock Margin="4 0 0 0" VerticalAlignment="Center" 
                                   FontSize="14" Text="Settings" />
                    </StackPanel>
                </DataTemplate>
            </Button.ContentTemplate>
        </Button>
        <Button x:Name="BtnSaveFile" Margin="8 0" 
                Command="{Binding ButtonSaveFileClick}" 
                CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
            <Button.ContentTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="8 0">
                        <fa:ImageAwesome Icon="Save" Width="16" Foreground="White" Margin="0 0 6 0" />
                        <TextBlock Margin="4 0 0 0" VerticalAlignment="Center" 
                                   FontSize="14" Text="Save" />
                    </StackPanel>
                </DataTemplate>
            </Button.ContentTemplate>
        </Button>
    </Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>

<Controls:HamburgerMenu Grid.Row="0" Grid.Column="0" 
    x:Name="HamburgerMenuControl" OpenPaneLength="360" IsPaneOpen="False" 
    DisplayMode="CompactOverlay" HamburgerWidth="60" CompactPaneLength="60" 
    ItemTemplate="{StaticResource HamburgerMenuItem}"
    ItemsSource="{Binding HamburgerMenuItems}" 
    OptionsItemTemplate="{StaticResource HamburgerOptionsMenuItem}" 
    OptionsItemsSource="{Binding HamburgerMenuOptionItems}" 
    ItemCommand="{Binding CmdHamburgerItemClicked}" 
    OptionsItemCommand="{Binding CmdHamburgerItemClicked}" >

    <!--  Header  -->
    <Controls:HamburgerMenu.HamburgerMenuHeaderTemplate>
        <DataTemplate>
            <TextBlock 
                HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="12 0 0 0"
                FontSize="18" FontWeight="DemiBold" Foreground="White" Text="Menu"/>
        </DataTemplate>
    </Controls:HamburgerMenu.HamburgerMenuHeaderTemplate>

    <!--  Content: User Control display container -->
    <Controls:HamburgerMenu.ContentTemplate>
        <DataTemplate>
            <Grid x:Name="HamburgerContentContainerGrid">
                <Controls:TransitioningContentControl
                    Content="{Binding}" RestartTransitionOnContentChange="True" Transition="Default">
                    <Controls:TransitioningContentControl.Resources>
                        <DataTemplate DataType="{x:Type Controls:HamburgerMenuGlyphItem}">
                            <ContentControl 
                                x:Name="UCDisplayContentControl" Content="{Binding Tag}" />
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type Controls:HamburgerMenuIconItem}">
                            <ContentControl 
                                x:Name="UCDisplayContentControl" Content="{Binding Tag}" />
                        </DataTemplate>
                    </Controls:TransitioningContentControl.Resources>
                </Controls:TransitioningContentControl>
            </Grid>
        </DataTemplate>
    </Controls:HamburgerMenu.ContentTemplate>

    <Controls:HamburgerMenu.Content>
        <MultiBinding Converter="{StaticResource selectedItemToContentConv}">
            <Binding 
                FallbackValue="{x:Null}" 
                Mode="OneWay"
                Path="SelectedItem"
                RelativeSource="{RelativeSource Self}" />
            <Binding 
                FallbackValue="{x:Null}" 
                Mode="OneWay" 
                Path="SelectedOptionsItem" 
                RelativeSource="{RelativeSource Self}" />
        </MultiBinding>
    </Controls:HamburgerMenu.Content>

</Controls:HamburgerMenu>
0

There are 0 best solutions below