我们所用到的桌面app,有左键菜单和右键菜单。在as和C#中同样分有左键菜单和右键菜单。而且API名字都一样(有部分不同),不过里边的属性不相同。
在C#中,右键菜单又叫快捷菜单。创建菜单,一种通过控件拖动进Grid中,一种用代码创建。也就是写在XAML中和写在cs中。
左键叫Menu,右键叫ContextMenu。左右键里边是MenuItem。
MenuItem有个属性,叫Header,这个东西啊,你会注意到有些前边叫了”_”。这个干嘛用呢。就是为了快速访问,用户可以通过Alt键 ,再 “-“之后的字母组合触发。
在这里要注意,在同一个菜单中,使用多个相同的访问键会让人迷糊。
对于控件。它的Name属性很重要。如果用AS来说,这个就是它的实例名称。
下别贴出左键的代码,看它的结构:
<Window x:Class="WPFMenu.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> <DockPanel Height="100" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="dockPanel1" VerticalAlignment="Top" Width="Auto"> <Menu Height="23" Name="menu1" DockPanel.Dock="Top" VerticalAlignment="Top"> <MenuItem Header="_File" > <MenuItem Header="_New File" Name="newFile"> <MenuItem.Icon> <Image Source="01.png" Width="16" Height="16"/> </MenuItem.Icon> </MenuItem> <MenuItem Header="_Save File" Name="saveFile"> <MenuItem.Icon> <Image Source="02.png" Width="16" Height="16"/> </MenuItem.Icon> </MenuItem> <MenuItem Header="_Exit" Name="exit" /> </MenuItem> <MenuItem Header="_Help" Name="help"> <MenuItem Header="About" Name="about"> <MenuItem.Icon> <Image Source="03.png" Width="16" Height="16"/> </MenuItem.Icon> </MenuItem> </MenuItem> </Menu> </DockPanel> <TextBox Height="147" HorizontalAlignment="Left" Margin="51,132,0,0" Name="box" VerticalAlignment="Top" Width="401" /> </Grid> </Window>
在左键的子菜单中,可以给菜单前边加icon图标。用到Image标签,给它的Source指定正确的图片路径。设置宽高就好了。
下边说下右键吧。右键是ContextMenu。子菜单的一样是MenuItem。创建子菜单,并且给子菜单添加事件。
MenuItem saveItem = new MenuItem(); saveItem.Header = "Save File"; saveItem.Click += saveHandler;
private void saveHandler(object sender, RoutedEventArgs e) { printf("想要保存File!"); }
然后将MenuItem添加到ContextMenu的Items中
ContextMenu contextMenu = new ContextMenu(); contextMenu.Items.Add(saveItem);
最后,创建的ContextMenu赋给窗体的contextMenu属性
this.ContextMenu = contextMenu;
在demo里边,about会触发一个窗体出来。
AboutForm aboutForm = new AboutForm(); aboutForm.ShowDialog();
虽然用ShowDialog()可以将窗体触发出来。只是位置太那个了点。因为对C#不熟悉,应该有方法可以让窗体显示到想要显示的位置的。
下别贴出完整demo代码。晚上回来敲的。学啊学C#。
MainWindow.xaml
<Window x:Class="WPFMenu.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> <DockPanel Height="100" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="dockPanel1" VerticalAlignment="Top" Width="Auto"> <Menu Height="23" Name="menu1" DockPanel.Dock="Top" VerticalAlignment="Top"> <MenuItem Header="_File" > <MenuItem Header="_New File" Name="newFile"> <MenuItem.Icon> <Image Source="01.png" Width="16" Height="16"/> </MenuItem.Icon> </MenuItem> <MenuItem Header="_Save File" Name="saveFile"> <MenuItem.Icon> <Image Source="02.png" Width="16" Height="16"/> </MenuItem.Icon> </MenuItem> <MenuItem Header="_Exit" Name="exit" /> </MenuItem> <MenuItem Header="_Help" Name="help"> <MenuItem Header="About" Name="about"> <MenuItem.Icon> <Image Source="03.png" Width="16" Height="16"/> </MenuItem.Icon> </MenuItem> </MenuItem> </Menu> </DockPanel> <TextBox Height="147" HorizontalAlignment="Left" Margin="51,132,0,0" Name="box" VerticalAlignment="Top" Width="401" /> </Grid> </Window>
MainWindow.xaml.cs
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 WPFMenu { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private ContextMenu contextMenu; public MainWindow() { InitializeComponent(); init(); } private void init() { MenuItem saveItem = new MenuItem(); saveItem.Header = "Save File"; saveItem.Click += saveHandler; MenuItem exitItem = new MenuItem(); exitItem.Header = "Exit"; exitItem.Click += exitHandler; contextMenu = new ContextMenu(); contextMenu.Items.Add(saveItem); contextMenu.Items.Add(new Separator()); //这个是分割线 contextMenu.Items.Add(exitItem); this.ContextMenu = contextMenu; newFile.Click += newFileHandler; saveFile.Click += saveHandler; exit.Click += exitHandler; about.Click += aboutHandler; } private void newFileHandler(object sender, RoutedEventArgs e) { printf("想要新建File!"); } private void saveHandler(object sender, RoutedEventArgs e) { printf("想要保存File!"); } private void exitHandler(object sender, RoutedEventArgs e) { printf("想要退出App!"); } private void aboutHandler(object sender, RoutedEventArgs e) { printf("想要关于,我就关于给你看!"); AboutForm aboutForm = new AboutForm(); aboutForm.ShowDialog(); } private void printf(string str) { box.AppendText("你当前的操作:" +str + "\n"); } } }
AboutForm.xaml
<Window x:Class="WPFMenu.AboutForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="AboutForm" Height="185" Width="294"> <Grid> <Label Content="VeryMagazine 1.0.0" Height="28" HorizontalAlignment="Left" Margin="34,23,0,0" Name="label1" VerticalAlignment="Top" /> <Label Content="春花秋月何时了,往事知多少!" Height="28" HorizontalAlignment="Left" Margin="34,57,0,0" Name="label2" VerticalAlignment="Top" FontSize="13" /> <Button Content="Ok" Height="23" HorizontalAlignment="Left" Margin="129,100,0,0" Name="okBtn" VerticalAlignment="Top" Width="75" /> </Grid> </Window>
AboutForm.xaml.cs
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; namespace WPFMenu { /// <summary> /// AboutForm.xaml 的交互逻辑 /// </summary> public partial class AboutForm : Window { public AboutForm() { InitializeComponent(); okBtn.Click += clickHandler; } private void clickHandler(object sender, RoutedEventArgs e) { this.Close(); } } }