Introduction
In XAML and the WPF model, when we simply want a File menu like Visual Studio's that has New, Open, Close, Exit, and a few other standard shortcuts, the Menu and the Menu Item classes represent a menu and a menu item respectively. A Menu is a collection of menu items with a command associated with each item. Child menu items are called sub menus. This article discusses how to work with menus in XAML and WPF applications.
Creating a Menu as Design Time:
<Window x:Class="Create_Menu.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="371" Width="606">
<Grid>
<Menu>
<MenuItem Header="File">
<MenuItem Header="New" />
<MenuItem Header="Open" IsCheckable="True" Click="Open_Click" />
<MenuItem Header="Save" Click="Save_Click"/>
<MenuItem Header="Close"></MenuItem>
<Separator/>
<MenuItem Header="Recent File">
<MenuItem Header="ajay.docx"></MenuItem>
<MenuItem Header="Raj.txt"></MenuItem>
</MenuItem>
<Separator/>
<MenuItem Header="Exit" Click="Exit_Click"/>
</MenuItem>
<MenuItem Header="Font">
<MenuItem.ToolTip>
<ToolTip>
Open a font menu.
</ToolTip>
</MenuItem.ToolTip>
<MenuItem Header="Bold" IsCheckable="True" Checked="Bold_Checked" Unchecked="Bold_Unchecked"/>
<MenuItem Header="Italic" IsCheckable="True" Checked="Italic_Checked" Unchecked="Italic_Unchecked"/>
<MenuItem Header="Thin" IsCheckable="True" Checked="Thin_Checked"/>
<MenuItem Header="Medium" IsCheckable="True" Checked="Medium_Checked"/>
<MenuItem Header="Increase Font Size" Click="IncreaseFont_Click"/>
<MenuItem Header="Decrease Font Size" Click="DecreaseFont_Click"/>
</MenuItem>
</Menu>
<Label Content="Welcome to MCN Solution" Height="28" HorizontalAlignment="Left" Margin="184,143,0,140" Name="lblmsg" />
</Grid>
</Window>
private void Open_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dl1 = new Microsoft.Win32.OpenFileDialog();
dl1.FileName = "MYFileSave";
dl1.DefaultExt = ".txt";
dl1.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dl1.ShowDialog();
if (result == true)
{
string filename = dl1.FileName;
}
}
private void Save_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dl1 = new Microsoft.Win32.SaveFileDialog();
dl1.FileName = "MYFileSave";
dl1.DefaultExt = ".txt";
dl1.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dl1.ShowDialog();
if (result == true)
{
string filename = dl1.FileName;
}
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Bold_Checked(object sender, RoutedEventArgs e)
{
lblmsg.FontWeight = FontWeights.Bold;
}
private void Bold_Unchecked(object sender, RoutedEventArgs e)
{
lblmsg.FontWeight = FontWeights.Normal;
}
private void Italic_Checked(object sender, RoutedEventArgs e)
{
lblmsg.FontStyle = FontStyles.Italic;
}
private void Italic_Unchecked(object sender, RoutedEventArgs e)
{
lblmsg.FontStyle = FontStyles.Normal;
}
private void IncreaseFont_Click(object sender, RoutedEventArgs e)
{
if (lblmsg.FontSize < 20)
{
lblmsg.FontSize += 2;
}
}
private void DecreaseFont_Click(object sender, RoutedEventArgs e)
{
if (lblmsg.FontSize > 10)
{
lblmsg.FontSize -= 2;
}
}
private void Thin_Checked(object sender, RoutedEventArgs e)
{
lblmsg.FontWeight = FontWeights.Thin;
}
private void Medium_Checked(object sender, RoutedEventArgs e)
{
lblmsg.FontWeight = FontWeights.Medium;
}
OUTPUT
So when we click on open, the output will be:
So when will click on save, the output will be:
The output with the tooltip looks like:
Here we used a checked event in some places, so the output will be like this: