Overview
In this article, we will see how to create menus in WPF and load its respective page or form. As there are many ways to create the menus in WPF, I had done it by using frame. Create a form and target that form on the frame. You can find many ways of creating menus in WPF .
Note
For those who are familiar with Windows Applications in Window form, we can create MDI. In WPF, we cannot create MDI forms.
So Let's Start
- Open Visual Studio
We will be using the same solution which we used for logging in to WPF using N tier architecture. You can download the existing solution here,
- Now, let's add the images. I had added images in images folder, shown below:
- After adding the images, we write the code for MainScreen, i.e Dashboardd, as shown below:
- <Window x:Class="The_Parcel.MainScreen"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Height="{Binding SystemParameters.PrimaryScreenHeight}"
- Width="{Binding SystemParameters.PrimaryScreenWidth}"
- Title="Dashbooard" Topmost="False" WindowStartupLocation="CenterScreen" WindowStyle="ThreeDBorderWindow" WindowState="Maximized" >
- As you can see, Title is Dashboard; give the suitable name that will appear on top of the screen.
- Windowstartuplocation: Centerscreen is a screen, which will open at the center.
- WindowState: Maximizes full mode sceren.
- WindowStartupLocation: By setting it to centerscreen, it will open in the middle of the sceren, when you run your solution .
Similarly WindowState: Maxmized and Minimized
- Maximized: The Window will open in the full screen mode, whereas Minimized will open in a small screen mode.
- Now, let's add an image background and imagebrush property, as shown below:
- <Window.Background>
- <ImageBrush ImageSource="Images/Simple-Orange-Wallpaper.jpg" />
- </Window.Background>
- <Window.Resources>
- <ImageBrush x:Key="Background1" ImageSource="Images/17_Booksellers_Most.jpg" />
- <ImageBrush x:Key="Background2" ImageSource="Images/promotion_new.png" />
- <ImageBrush x:Key="Background3" ImageSource="Images/smsalerts.jpg" />
- <ImageBrush x:Key="Background4" ImageSource="Images1/reports.jpg" />
- <ImageBrush x:Key="Background5" ImageSource="Images1/xyz.png" />
- <ImageBrush x:Key="Background6" ImageSource="Images1/partyorders.jpg" />
- <ImageBrush x:Key="Background7" ImageSource="Images/help_balloon.png" />
- <ImageBrush x:Key="Background8" ImageSource="Images1/neworder.jpg" />
- <ImageBrush x:Key="Background9" ImageSource="Images1/inventory.png" />
- <ImageBrush x:Key="Background10" ImageSource="Images1/Loyalcustomers.jpg" />
- <ImageBrush x:Key="Background11" ImageSource="Images1/oie_3svVKQ9e5T5o.png" />
- </Window.Resources>
- Remember the keys for which you are specifying for the button.
- Now add the buttons.
We have specified the keys for each button, as shown below:
- <ImageBrush x:Key="Background2" ImageSource="Images/promotion_new.png" />
Now, by adding the buttons, let's bind those keys by {staticResource ResourceKey=”ItsBackground”},
- <Viewbox Stretch="Uniform">
- <Grid Height="950" Width="1500">
- <Grid Height="950">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="435*" />
- <ColumnDefinition Width="723*" />
- </Grid.ColumnDefinitions>
- <Button Background="{StaticResource ResourceKey=Background3}" ToolTip="Send SMS and Email." Height="91" HorizontalAlignment="Left" Name="btnSMSEmail" VerticalAlignment="Top" Width="100" Click="btnSMSEmail_Click" Grid.Column="1" Margin="19,8,0,0">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background5}" ToolTip="Manage employee data." Height="92" HorizontalAlignment="Left" Margin="473,7,0,0" Name="btnEmpDetails" VerticalAlignment="Top" Width="90" Click="btnEmpDetails_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background7}" ToolTip="Need help click here." Height="91" HorizontalAlignment="Left" Margin="134,8,0,0" Name="btnHelp" VerticalAlignment="Top" Width="92" Grid.Column="1" Click="btnHelp_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background8}" Height="91" ToolTip="Place New Order" HorizontalAlignment="Left" Margin="14,7,0,0" Name="btnOrder" VerticalAlignment="Top" Width="92" Click="btnOrder_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background9}" ToolTip="View Reports" Height="91" HorizontalAlignment="Left" Margin="132,7,0,0" Name="btnInventory" VerticalAlignment="Top" Width="92" Click="btnInventory_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background10}" ToolTip="View your customers." Height="92" HorizontalAlignment="Left" Margin="365,7,0,0" Name="btnCustomer" VerticalAlignment="Top" Width="92" Click="btnCustomer_Click" Grid.ColumnSpan="2">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background11}" ToolTip="Party orders coming soon.." Height="92" HorizontalAlignment="Left" Margin="250,6,0,0" Name="btnPartyOrders" VerticalAlignment="Top" Width="92" Click="btnPartyOrders_Click_1">
-
- </Button>
- Hence, it's going to look like the menus now, as shown below:
- Now, let's add a frame in which our respective form will be displayed.
A normal <Frame></Frame> Tag will now be created here. I had specified some properties like vertical alignment, width and so on. You can explore more properties.
- VerticalAlignment="Stretch" Width="1480" NavigationUIVisibility="Hidden" Grid.ColumnSpan="2"
- <Frame Height="900" HorizontalAlignment="Left" Name="frame1" VerticalAlignment="Stretch" Width="1480" NavigationUIVisibility="Hidden" Grid.ColumnSpan="2" Margin="6,104,0,6"></Frame>
- Final Code
MainScren.Xaml
- <Window x:Class="The_Parcel.MainScreen"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Height="{Binding SystemParameters.PrimaryScreenHeight}"
- Width="{Binding SystemParameters.PrimaryScreenWidth}"
- Title="Dashbooard" Topmost="False" WindowStartupLocation="CenterScreen" WindowStyle="ThreeDBorderWindow" WindowState="Maximized" >
- <Window.Background>
- <ImageBrush ImageSource="Images/Simple-Orange-Wallpaper.jpg" />
- </Window.Background>
- <Window.Resources>
- <ImageBrush x:Key="Background1" ImageSource="Images/17_Booksellers_Most.jpg" />
- <ImageBrush x:Key="Background2" ImageSource="Images/promotion_new.png" />
- <ImageBrush x:Key="Background3" ImageSource="Images/smsalerts.jpg" />
- <ImageBrush x:Key="Background4" ImageSource="Images1/reports.jpg" />
- <ImageBrush x:Key="Background5" ImageSource="Images1/xyz.png" />
- <ImageBrush x:Key="Background6" ImageSource="Images1/partyorders.jpg" />
- <ImageBrush x:Key="Background7" ImageSource="Images/help_balloon.png" />
- <ImageBrush x:Key="Background8" ImageSource="Images1/neworder.jpg" />
- <ImageBrush x:Key="Background9" ImageSource="Images1/inventory.png" />
- <ImageBrush x:Key="Background10" ImageSource="Images1/Loyalcustomers.jpg" />
- <ImageBrush x:Key="Background11" ImageSource="Images1/oie_3svVKQ9e5T5o.png" />
- </Window.Resources>
- <Viewbox Stretch="Uniform">
- <Grid Height="950" Width="1500">
- <Grid Height="950">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="435*" />
- <ColumnDefinition Width="723*" />
- </Grid.ColumnDefinitions>
- <Button Background="{StaticResource ResourceKey=Background3}" ToolTip="Send SMS and Email." Height="91" HorizontalAlignment="Left" Name="btnSMSEmail" VerticalAlignment="Top" Width="100" Click="btnSMSEmail_Click" Grid.Column="1" Margin="19,8,0,0">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background5}" ToolTip="Manage employee data." Height="92" HorizontalAlignment="Left" Margin="473,7,0,0" Name="btnEmpDetails" VerticalAlignment="Top" Width="90" Click="btnEmpDetails_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background7}" ToolTip="Need help click here." Height="91" HorizontalAlignment="Left" Margin="134,8,0,0" Name="btnHelp" VerticalAlignment="Top" Width="92" Grid.Column="1" Click="btnHelp_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background8}" Height="91" ToolTip="Place New Order" HorizontalAlignment="Left" Margin="14,7,0,0" Name="btnOrder" VerticalAlignment="Top" Width="92" Click="btnOrder_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background9}" ToolTip="View Reports" Height="91" HorizontalAlignment="Left" Margin="132,7,0,0" Name="btnInventory" VerticalAlignment="Top" Width="92" Click="btnInventory_Click">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background10}" ToolTip="View your customers." Height="92" HorizontalAlignment="Left" Margin="365,7,0,0" Name="btnCustomer" VerticalAlignment="Top" Width="92" Click="btnCustomer_Click" Grid.ColumnSpan="2">
-
- </Button>
- <Button Background="{StaticResource ResourceKey=Background11}" ToolTip="Party orders coming soon.." Height="92" HorizontalAlignment="Left" Margin="250,6,0,0" Name="btnPartyOrders" VerticalAlignment="Top" Width="92" Click="btnPartyOrders_Click_1">
-
- </Button>
-
- <Frame Height="900" HorizontalAlignment="Left" Name="frame1" VerticalAlignment="Stretch" Width="1480" NavigationUIVisibility="Hidden" Grid.ColumnSpan="2" Margin="6,104,0,6"></Frame>
- </Grid>
-
- </Grid>
- </Viewbox>
- </Window>
- Double click on the each button and you will see as shown below:
(you need to specify the action for the buttons OnClick).
Now, let's add a simple form, as shown below:
- Select the page, as shown below:
Give Suitable Name
Add what you want as I had added a software version
Page2.Xaml
- <Page x:Class="The_Parcel.Page1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- mc:Ignorable="d"
- d:DesignHeight="192" d:DesignWidth="469"
- Title="Page1">
-
- <Grid Height="958" Width="604">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="7*" />
- <ColumnDefinition Width="1169*" />
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="361*" />
- <RowDefinition Height="597*" />
- </Grid.RowDefinitions>
- <RichTextBox SpellCheck.IsEnabled="True" Margin="4,8,0,43" Name="RichTextBox1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="588" Height="310" FontSize="18" FontFamily="Tahoma" IsReadOnly="True" Grid.ColumnSpan="2" ScrollViewer.HorizontalScrollBarVisibility="Visible" >
- <FlowDocument>
- <Paragraph>
- <Bold FontFamily="Tahoma">Welcome To Parcel V 1.0 .</Bold>
- </Paragraph>
-
-
-
- </FlowDocument>
- </RichTextBox>
- </Grid>
- </Page>
- Now, let's move back to Mainscreen.xaml.cs and just write as shown below:
- private void btnHelp_Click(object sender, RoutedEventArgs e)
- {
- frame1.Source = new Uri("Page2.xaml", UriKind.Relative);
- }
- Final Mainscrren.xaml.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using System.Data.SqlClient;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
-
-
- namespace The_Parcel
- {
-
-
-
- public partial class MainScreen : Window
- {
-
-
-
-
-
- public MainScreen()
- {
- InitializeComponent();
- }
-
- private void btnOrder_Click(object sender, RoutedEventArgs e)
- {
-
-
-
- }
-
- private void btnEmpDetails_Click(object sender, RoutedEventArgs e)
- {
-
- }
-
- private void btnSMSEmail_Click(object sender, RoutedEventArgs e)
- {
-
- }
-
- private void btnCustomer_Click(object sender, RoutedEventArgs e)
- {
-
- }
-
- private void btnPartyOrders_Click_1(object sender, RoutedEventArgs e)
- {
-
- MessageBox.Show("Coming Soon.......");
- }
-
- private void btnHelp_Click(object sender, RoutedEventArgs e)
- {
- frame1.Source = new Uri("Page2.xaml", UriKind.Relative);
- }
-
- private void btnInventory_Click(object sender, RoutedEventArgs e)
- {
-
- }
- }
- }
Hence, our final dashboard will look as shown below:
Conclusion
Here are the menus in WPF (simple menus). I hope this article was helpful.