Designing a Hamburger menu in WPF with Mahapps.Metro

10,859

Well, I figured it out myself.

Here's the code:

<DockPanel>        
    <DockPanel.Resources>            
        <DataTemplate x:Key="MenuItemTemplate">
            <Grid Height="48" Background="Black">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <cal:ActionMessage MethodName="{Binding Tag}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="48"/>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe MDL2 Assets" Foreground="BlanchedAlmond" Text="{Binding Glyph}"/>
                <TextBlock Grid.Column="1" VerticalAlignment="Center" FontSize="16" Foreground="White" Text="{Binding Label}"/>
            </Grid>
        </DataTemplate>
    </DockPanel.Resources>

    <Controls:HamburgerMenu Foreground="White" PaneBackground="#FF444444" IsPaneOpen="False" DisplayMode="CompactOverlay" OptionsItemTemplate="{StaticResource MenuItemTemplate}" ItemTemplate="{StaticResource MenuItemTemplate}">
        <Controls:HamburgerMenu.ItemsSource>
            <Controls:HamburgerMenuItemCollection>
                <Controls:HamburgerMenuGlyphItem Glyph="M" Label="Main Menu" Tag="ShowMainMenu"/>
                <Controls:HamburgerMenuGlyphItem Glyph="I" Label="Invoice" Tag="OpenInvoiceMaker"/>
                <Controls:HamburgerMenuGlyphItem Glyph="R" Label="Reports" Tag="ShowReportsMenu"/>
            </Controls:HamburgerMenuItemCollection>
        </Controls:HamburgerMenu.ItemsSource>
        <Controls:HamburgerMenu.OptionsItemsSource>
            <Controls:HamburgerMenuItemCollection>
                <Controls:HamburgerMenuGlyphItem Glyph="A" Label="About" Tag="About"/>
            </Controls:HamburgerMenuItemCollection>
        </Controls:HamburgerMenu.OptionsItemsSource>
        <Controls:HamburgerMenu.Content>
            <DockPanel>
                <Border Background="#FF444444" DockPanel.Dock="Top">
                    <TextBlock x:Name="Header" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Foreground="White" Text="{Binding ActiveItem.DisplayName}" />
                </Border>
                <ScrollViewer Grid.Row="1" CanContentScroll="True" IsDeferredScrollingEnabled="False">
                    <Controls:TransitioningContentControl Transition="LeftReplace" x:Name="ActiveItem" Content="{Binding ActiveItem}"/>
                </ScrollViewer>
            </DockPanel>
        </Controls:HamburgerMenu.Content>
    </Controls:HamburgerMenu>
</DockPanel>

Basically, I used EventTrigger to achieve the same functionality as a button. The rest was easy.

Share:
10,859
Sabyasachi Mukherjee
Author by

Sabyasachi Mukherjee

Updated on June 17, 2022

Comments

  • Sabyasachi Mukherjee
    Sabyasachi Mukherjee almost 2 years

    Currently, I'm using a Grid of Buttons for housing keys for navigation. It's hideous, but gets the job done.

    Here's the XAML chunk:

    <Button x:Name="ShowMainMenu" Grid.Column="0" Margin="5" Content="Main Menu"/>
    <Button x:Name="ShowReportsMenu" Grid.Column="2" Margin="5" Content="Reports"/>
    <Button x:Name="Reset" Grid.Column="4" Margin="5" Content="Reset"/>
    <Button x:Name="TryClose" Grid.Column="6" Margin="5" Content="Close"/>
    

    Incidentally, I'm using Caliburn.micro, and so naming the buttons is enough to bind them to proper commands.

    Now, I want them to put them in a Hamburger Menu.

    What I have tried till now is this:

    <Controls:HamburgerMenu >
        <Controls:HamburgerMenu.ItemTemplate>
                <DataTemplate DataType="{x:Type Controls:HamburgerMenuGlyphItem}">
                    <Grid Height="48">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="48" />
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Margin="12" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Glyph}" />
                        <TextBlock Grid.Column="1" VerticalAlignment="Center" FontSize="16" Text="{Binding Label}" />                            
                    </Grid>
                </DataTemplate>                
        </Controls:HamburgerMenu.ItemTemplate>
        <Controls:HamburgerMenu.ItemsSource>
            <Controls:HamburgerMenuItemCollection>
                <Controls:HamburgerMenuGlyphItem Glyph="1" Label="Main Menu" cal:Message.Attach="[Event Click] = [Action ShowMainMenu]"/>
                </Controls:HamburgerMenuItemCollection>
        </Controls:HamburgerMenu.ItemsSource>
    </Controls:HamburgerMenu>
    

    But it doesn't work, because Controls:HamburgerMenuGlyphItem isn't a FrameworkElement.

    By the way, the transitioning view is implemented like this:

    So, how can I translate my bunch of Buttons into a HamburgerMenu? I can't find proper documentation for Caliburn.micro's HamburgerMenu as well.