Creating A "Hamburger" Menu for Windows 10 UWP Apps

Updated on November 17, 2016

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.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Harry 

      2 years ago

      Very helpful!Thank you!

    • profile image

      Ombrok 

      2 years ago

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

    • profile image

      Sven 

      2 years ago

      Great! Thank you.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)