<window x:class="WPF_Menus.MainWindow" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <stackpanel> <menu> <menuitem header="File"> <!--Adorned Menu Item New Game with an Image Can also set the IsChecked or IsCheckable Property. Menus also contain and area for Keyboard shortcuts but they still need to be setup in the code behind.--> <menuitem header="New Game" click="MenuItemNewGame_Click" inputgesturetext="Alt+N"> <menuitem.icon> <Image Source="Card.jpg"/> </menuitem.icon> </menuitem> <!--Example of Other content as the Menu Header in this Case an Image--> <menuitem inputgesturetext="Alt+Q"> <menuitem.header> <Image Source="Ace.jpg"/> </menuitem.header> </menuitem> <!--MenuItem with Commands Attached. The Benefits are that the Text Header is automatically set, The keyboard shortcut is filled with the appropriate string, and the command is hooked up so if the user uses the MenuItem or the keyboard shortcut the command is executed. This assumes that you've bound the command correctly in the code behind. I've made this a Nested MenuItem.--> <menuitem header="File"> <menuitem command="ApplicationCommands.New" /> <menuitem command="ApplicationCommands.Open" /> </menuitem> <menuitem header="Exit" click="MenuItemExit_Click" /> </menuitem> <menuitem header="Help" click="MenuItemHelp_Click" /> </menu> </stackpanel> </window>
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.Navigation; using System.Windows.Shapes; namespace WPF_Menus { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //Bindings for the Nested MenuItem's New and Open CommandBinding nBinding = new CommandBinding(); nBinding.Command = ApplicationCommands.New; nBinding.Executed += DoNew_Executed; nBinding.CanExecute += DoNew_CanExecute; CommandBinding oBinding = new CommandBinding(); oBinding.Command = ApplicationCommands.Open; oBinding.Executed += DoOpen_Executed; oBinding.CanExecute += DoOpen_CanExecute; } //Regular Menu Event Handlers private void MenuItemNewGame_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Clicked New Game", "Menu Info"); } private void MenuItemExit_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Clicked Exit", "Menu Info"); } private void MenuItemHelp_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Clicked Help", "Menu Info"); } //Event handlers for the Nested MenuItem's New and Open. public void DoNew_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("New Command Executed", "Command Info"); } public void DoNew_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } public void DoOpen_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Open Command Executed", "Command Info"); } public void DoOpen_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } } }
System.Windows.Input.CommandBinding.CanExecute
System.Windows.Input.CanExecuteRoutedEventArgs
CanExecute
System.Windows.UIElement.IsEnabled
UIElement
Window.CommandBindings.Add
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)