report

Creating A "Hamburger" Menu for Windows 10 UWP Apps

How This Came About

With the official release of Windows 10, I decided I should take the plunge and try to start writing "modern" apps. I am firmly rooted in winforms development, so XAML has been a huge source of frustration because it takes a completely different mindset to design the UI. I looked for examples on building a navigation menu for a UWP (Universal Windows Platform) App and everything that I found was entirely too complicated for what I was wanting to do.

I finally figured out a way to do what I wanted in a fairly simple way, but since I am still new to UWP and XAML if anyone has suggestions for improvement be sure to post them in the comments!

Default Menu Display
Default Menu Display

Step 1: The Menu Model

In your app (you can create a blank UWP App if you do not already have one), add a Models folder (if it doesn't already exist). In this folder you will create two classes NavItem and NavMenu. Also go ahead and create a Views folder, as this will hold the pages for your app.

NavItem is where we will hold the data for each item in our menu. It is fairly basic, taking three parameters for holding the unicode character for a menu glyph, the text for the menu item, and the page that the menu will load.

 
using MyApp.Views;
using System;

namespace MyApp.Models
{
    public class NavItem
    {
        public object Glyph { get; set; }
        public string Text { get; set; }
        public Type Page { get; set; }

        public NavItem(object glyph = null, string text = "", Type page = null)
        {
            Glyph = glyph;
            Text = text;
            Page = (page == null ? typeof(BlankPage) : page);
        }
    }
}

NavMenu is where a lot of the magic happens as it is where you specify the background color for your menu (foreground and highlight colors can be added as well if you wish), as well as the menu items to be displayed.

using MyApp.Views; 
using System.Collections.Generic; 

namespace MyApp.Models { 
    public class NavMenu { 
        public string BackgroundColor { get; set; } 
        public List MenuItems { get; set; } 
        public NavMenu() { 
            Color = "#FF2B2B2B"; 
            MenuItems = new List() { 
                new NavItem("\uE1CE","Item1", typeof(Page1)), 
                new NavItem("\uE1CF","Item2", typeof(Page2)), 
                new NavItem("\uE104","Item3", typeof(Page3)), 
                new NavItem("\uE104","Item4", typeof(Page4)) 
            }; 
        } 
    } 
}

Note that Page1, Page2, Page3, and Page4 are Pages (not Views!) in the Views folder...you could change it to be the Pages folder instead and use the namespace MyApp.Pages instead.

Step 2: Displaying the Menu

The way I have set the menu up, it occupies the left side of the application using a SplitView control. The SplitView Content area contains a Frame that will display the selected page once a menu item is selected.

The SplitView Pane contains the menu items which are housed within a couple of StackPanels. The Pane is aligned to the Top and Left of the windows, and is collapsed by default. When the menu is opened, it will open on top of the page content (instead of pushing everything to the side).

The first (Vertical) StackPanel has its DataContext assigned to the NavMenu class that we created in Step 1. This allows us to set properties of child controls using the {Binding } syntax.

Next, we have a (Horizontal) StackPanel that contains the Button that will open and close our menu. There is also a TextBlock to specify a menu header.

Under this is our ListBox that will display the menu items specified in the NavMenu class. Its ItemSource is bound to the NavMenu.MenuItems property, which is a List of NavItem objects. The ListBox items are rendered as a (Horizontal) StackPanel containing a TextBlock for our glyph and a TextBlock for the menu text.

<Page
    x:Class="MyApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:vm="using:MyApp.Models"
    mc:Ignorable="d">

    <SplitView x:Name="NavStrip" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        DisplayMode="CompactOverlay" 
        IsPaneOpen="False">
        <SplitView.Pane>
            <StackPanel Orientation="Vertical" 
                Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                <StackPanel.DataContext>
                    <vm:NavMenu/>
                </StackPanel.DataContext>
                <StackPanel Orientation="Horizontal" 
                    Background="{Binding BackgroundColor}">
                    <Button x:Name="NavButton" 
                        Content="" 
                        FontFamily="Segoe MDL2 Assets" 
                        Width="48" 
                        Height="36" 
                        Click="NavButton_Click" 
                        Background="Transparent" 
                        HorizontalAlignment="Left" />
                    <TextBlock Text="MY APP" Margin="16,8,0,0" />
                </StackPanel>                
                <ListBox x:Name="NavMenu" 
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Top" 
                    SelectionChanged="NavMenu_SelectionChanged" 
                    Background="{Binding BackgroundColor}" 
                    ItemsSource="{Binding MenuItems}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock x:Name="Glyph" 
                                    Text="{Binding Glyph}" 
                                    FontFamily="Segoe MDL2 Assets". 
                                    Width="48" 
                                    Margin="5,0,0,0" />
                                <TextBlock Text="{Binding Text}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </SplitView.Pane>
        <SplitView.Content>
            <Frame></Frame>           
        </SplitView.Content>
    </SplitView>    
</Page>

 


Step 3: Events

We only have a couple of events to add to the MainPage codebehind to make this fully functional.

NavButton_Click simply toggles the menu page open and closed.

NavMenu_SelectionChanged Gets the selected NavItem.Page value and passes it to the NavStrip.Content Frame so that it can be displayed.

private void NavButton_Click(object sender, RoutedEventArgs e)
{
    NavStrip.IsPaneOpen = !NavStrip.IsPaneOpen;
}

private void NavMenu_SelectionChanged(object sender, 
    SelectionChangedEventArgs e)
{
    ListBox list = (ListBox)sender;
    if(list.SelectedIndex == -1) { return; }
    Frame current = NavStrip.Content as Frame;            
    current.Navigate(((NavItem)list.SelectedItem).Page);
    NavStrip.IsPaneOpen = false;            
}

Menu Open View
Menu Open View

Conclusion

That's it! While it still seems like a lot of work, it is a lot easier than the other tutorials out there. I would like to improve upon this by changing from {Binding } to {x:Bind } so that it will perform more efficiently.

Feel free to ask questions in the comments and I will answer them the best that I can.

3 comments

Sven 15 months ago

Great! Thank you.


Ombrok 15 months ago

Thank you alot; its been 2 days that i'm looking for a good tutorial


Harry 5 months ago

Very helpful!Thank you!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article

    Menu

    Explore