The Menu Control defines a menu of choices for users to invoke, it is inheriting from Item Control. The default ItemsPanel for the menu control is WrapPanel and it only supports MenuItem as an item\children. The Menu Control positions its items the way the WrapPanel does based on the selected orientation Vertical\Horizontal (Developers can change the control ItemsPanel). The Menu items must be of type MenuItem, and each MenuItem can be opened using keyboard or pointer.
Menu Control is from UWP Community Toolkit. The UWP Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP apps for Windows 10.
Reading this article, you can learn how to use Menu Control in Universal Windows Apps development with XAML and Visual C#.
The following important tools are required for developing UWP,
- Windows 10 (Recommended)
- Microsoft Visual Studio 2017 (https://www.visualstudio.com/vs/whatsnew/ )
- Windows Anniversary Update (10.0.14393.0) or higher is needed to support correctly this feature.
Step 1
Open Visual studio 2017 -> Start -> New Project-> Select Windows Universal (under Visual C#)-> Blank App(Universal Windows) -> Give the Suitable Name for your App (UWPMenu)->OK.
After Choosing the Target and minimum platform version for your Windows Universal Application will support (Windows Anniversary Update (10.0.14393.0)) the Project creates App.xaml and MainPage.xaml.
Step 2
First update the reference, Microsoft.Toolkit.UWP.UI with the latest version of Manage NuGet Packages
Step 3
Add the following controls in design window for MenuControl feature view.
Add the Grid, TextBlock Control and change the Name and Text Property
Add the following 4 class file for NewProjectCommand.cs, OpenCommand.cs, copy.cs, cut.cs command execution.
Add the following namespace and code in NewProjectCommand.cs.
- using System.Windows.Input;
- using Windows.UI.Popups;
- internal class NewProjectCommand: ICommand {
- public bool CanExecute(object parameter) {
- return true;
- }
- public async void Execute(object parameter) {
- var dialog = new MessageDialog("Create New Project");
- await dialog.ShowAsync();
- }
- public event EventHandler CanExecuteChanged;
- }
Add the following namespace and code in OpenCommand.cs,
- using System.Windows.Input;
- using Windows.UI.Popups;
- internal class OpenCommand: ICommand {
- public bool CanExecute(object parameter) {
- return true;
- }
- public async void Execute(object parameter) {
- var dialog = new MessageDialog("Open Existing Project");
- await dialog.ShowAsync();
- }
- public event EventHandler CanExecuteChanged;
- }
Add the following namespace and code in copy.cs,
- using System.Windows.Input;
- using Windows.UI.Popups;
- internal class Copy: ICommand {
- public bool CanExecute(object parameter) {
- return true;
- }
- public async void Execute(object parameter) {
- var dialog = new MessageDialog("Copy content");
- await dialog.ShowAsync();
- }
- public event EventHandler CanExecuteChanged;
- }
Add the following namespace and code in cut.cs
- using System.Windows.Input;
- using Windows.UI.Popups;
- internal class Cut: ICommand {
- public bool CanExecute(object parameter) {
- return true;
- }
- public async void Execute(object parameter) {
- var dialog = new MessageDialog("Cut content");
- await dialog.ShowAsync();
- }
- public event EventHandler CanExecuteChanged;
- }
Add Page Resource for each command,
- <Page.Resources>
- <ResourceDictionary>
- <local:NewProjectCommand x:Key="NewProject" />
- <local:OpenCommand x:Key="OpenProject" />
- <local:Copy x:Key="Copycontent" />
- <local:Cut x:Key="Cutcontent" /> </ResourceDictionary>
- </Page.Resources>
Add the MenuControl with namespace and two Menu Items File and Edit with MenuFlyoutSubItem
- <xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" <controls:Menu AllowTooltip="False" Orientation="Horizontal" Background="#FFEDF6F7" FontFamily="Segoe UI" FlowDirection="LeftToRight" Margin="0,10,229,-2621" Grid.Row="1" Grid.RowSpan="2">
- <controls:MenuItem Name="FileMenu" controls:Menu.InputGestureText="Alt+F" Header="^File" FontSize="20" Margin="0,10,0,-10" HorizontalAlignment="Left" Width="50">
- <MenuFlyoutSubItem Text="New">
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+Shift+N" Text="Project" Command="{StaticResource NewProject}" /> </MenuFlyoutSubItem>
- <MenuFlyoutSeparator/>
- <MenuFlyoutSubItem Text="Open">
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+Shift+O" Text="OpenProject" Command="{StaticResource OpenProject}" /> </MenuFlyoutSubItem>
- </controls:MenuItem>
- </controls:Menu>
- <controls:Menu AllowTooltip="False" Orientation="Vertical" Background="#FFEDF6F7" FontFamily="Segoe UI" FlowDirection="LeftToRight" Margin="51,10,147,-7937" Grid.Row="1" Grid.RowSpan="2">
- <controls:MenuItem Name="Edit" controls:Menu.InputGestureText="Alt+E" Header="^Edit" FontSize="20" Margin="0,10,9,-10">
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+C" Text="Copy" Command="{StaticResource Copycontent}" />
- <MenuFlyoutSeparator/>
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+X" Text="Cut" Command="{StaticResource Cutcontent}" /> </controls:MenuItem>
- </controls:Menu>
Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.
- <Page x:Class="UWPMenu.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPMenu" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" mc:Ignorable="d">
- <Page.Resources>
- <ResourceDictionary>
- <local:NewProjectCommand x:Key="NewProject" />
- <local:OpenCommand x:Key="OpenProject" />
- <local:Copy x:Key="Copycontent" />
- <local:Cut x:Key="Cutcontent" /> </ResourceDictionary>
- </Page.Resources>
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10,10,1045,180">
- <Grid.RowDefinitions>
- <RowDefinition Height="auto" MinHeight="52" />
- <RowDefinition Height="auto" />
- <RowDefinition/> </Grid.RowDefinitions>
- <controls:Menu AllowTooltip="False" Orientation="Horizontal" Background="#FFEDF6F7" FontFamily="Segoe UI" FlowDirection="LeftToRight" Margin="0,10,229,-2621" Grid.Row="1" Grid.RowSpan="2">
- <controls:MenuItem Name="FileMenu" controls:Menu.InputGestureText="Alt+F" Header="^File" FontSize="20" Margin="0,10,0,-10" HorizontalAlignment="Left" Width="50">
- <MenuFlyoutSubItem Text="New">
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+Shift+N" Text="Project" Command="{StaticResource NewProject}" /> </MenuFlyoutSubItem>
- <MenuFlyoutSeparator/>
- <MenuFlyoutSubItem Text="Open">
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+Shift+O" Text="OpenProject" Command="{StaticResource OpenProject}" /> </MenuFlyoutSubItem>
- </controls:MenuItem>
- </controls:Menu>
- <controls:Menu AllowTooltip="False" Orientation="Vertical" Background="#FFEDF6F7" FontFamily="Segoe UI" FlowDirection="LeftToRight" Margin="51,10,147,-7937" Grid.Row="1" Grid.RowSpan="2">
- <controls:MenuItem Name="Edit" controls:Menu.InputGestureText="Alt+E" Header="^Edit" FontSize="20" Margin="0,10,9,-10">
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+C" Text="Copy" Command="{StaticResource Copycontent}" />
- <MenuFlyoutSeparator/>
- <MenuFlyoutItem controls:Menu.InputGestureText="Ctrl+X" Text="Cut" Command="{StaticResource Cutcontent}" /> </controls:MenuItem>
- </controls:Menu>
- <TextBlock x:Name="tbltitle_Copy" HorizontalAlignment="Left" Margin="37,10,0,0" Text="Menu Control in UWP" TextWrapping="Wrap" VerticalAlignment="Top" Height="42" Width="260" FontSize="24" FontWeight="Bold" FontFamily="Segoe UI" /> </Grid>
- </Page>
Step 4
Deploy your app in a local machine, and the output of the UWPMenu is:
While selecting File ->New->Project
After Clicking Project:
While selecting File ->Open->OpenProject
After Clicking OpenProject:
While selecting Edit Menu:
After clicking Copy:
After clicking Cut:
Summary
Now you have successfully tested Menu control in XAML and UWP environment using Visual Studio 2017.