void Loaded(object sender, RoutedEventArgs e) { ContextMenu contextMenu = new ContextMenu(); contextMenu.Background = new SolidColorBrush(Colors.Transparent); MenuItem item1 = new MenuItem(); item1.Header = "红色"; item1.Click += new RoutedEventHandler(item_Click); contextMenu.Items.Add(item1); MenuItem item2 = new MenuItem(); item2.Header = "蓝色"; item2.Click += new RoutedEventHandler(item_Click); contextMenu.Items.Add(item2); MenuItem item3 = new MenuItem(); item3.Header = "绿色"; item3.Click += new RoutedEventHandler(item_Click); contextMenu.Items.Add(item3); ContextMenuService.SetContextMenu(txtName, contextMenu); } void item_Click(object sender, RoutedEventArgs e) { MenuItem item = sender as MenuItem; //ContextMenu parent = item.Parent as ContextMenu; switch (item.Header.ToString()) { case "红色": ellipse1.Fill = new SolidColorBrush(Colors.Red); break; case "蓝色": ellipse1.Fill = new SolidColorBrush(Colors.Blue); break; case "绿色": ellipse1.Fill = new SolidColorBrush(Colors.Green); break; default: break; } }
自己玩的简单右键菜单
xaml
<ListView.ContextMenu> <ContextMenu> <MenuItem Header="下载" Click="MenuItem_Click" /> <MenuItem Header="打开" /> </ContextMenu> </ListView.ContextMenu>
c# 关于Ftp.FTPHelper,看另有一篇随笔很实用的FTP操作类
private void MenuItem_Click(object sender, RoutedEventArgs e) { Ftp.FTPHelper("pic", "123456"); Ftp.DownLoadFile(ftp://192.168.1.1/ + str, "e:\" + str); System.Windows.Forms.MessageBox.Show("下载完毕"); }
再使用IsEnbled的属性,又一强暴功能