C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: Right-click on the Menu element in Visual Studio to add MenuItems. The first MenuItems you add are the top-level menus.
Header: The Header attribute is the text that is shown. For commands that show a dialog box, three periods (an ellipsis) is standard.
Separator: A Separator too can be added. You will probably need to adjust the height of the Separator—removing it makes it the default.
Example markup: XAML
<Window x:Class="WpfApplication21.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Menu HorizontalAlignment="Left" VerticalAlignment="Top" Width="517">
<MenuItem Header="Info">
<MenuItem Header="One..." HorizontalAlignment="Left" Width="140"
Click="MenuItem_Click"/>
<MenuItem Header="Two..." HorizontalAlignment="Left" Width="140"
Click="MenuItem_Click"/>
<Separator HorizontalAlignment="Left" Width="140"/>
<MenuItem Header="Three..." HorizontalAlignment="Left" Width="140"
Click="MenuItem_Click"/>
</MenuItem>
</Menu>
</Grid>
</Window>
Example code: C#
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication21
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
// ... Cast sender object.
MenuItem item = sender as MenuItem;
// ... Change Title of this window.
this.Title = "Info: " + item.Header;
}
}
}
Note: The same method (MenuItem_Click) is used for all the MenuItems. A different method could be used instead, if needed.