windows phone上下文菜单ContextMenu的使用示例
- 新建一个WP项目,命名为contextmenu,然后往界面拖入一个ListBox控件listBox1,接着切换到XAML代码界面设置其属性,代码如下
<ListBox Height="437" HorizontalAlignment="Left" Margin="10,119,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock FontSize="40" Text="{Binding MyName}"/> <TextBlock FontSize="40" Text="{Binding Company}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
2.设置好上述属性后,在<TextBlock FontSize="40" Text="{Binding Company}"/>下边拖放上下文控件ContextMenu,完整代码如下:
<ListBox Height="437" HorizontalAlignment="Left" Margin="10,119,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock FontSize="40" Text="{Binding MyName}"/> <TextBlock FontSize="40" Text="{Binding Company}"/> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="变色" Click="MenuItem_Click"/> <toolkit:MenuItem Header="删除" Click="MenuItem_Click"/> <toolkit:MenuItem Header="全部删除" Click="MenuItem_Click"/> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
3.切换到.CS后台代码,加入命名空间的引用:using System.Collections.ObjectModel;
4.本例的完整后台C#代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.Collections.ObjectModel; namespace contextmenu { public partial class MainPage : PhoneApplicationPage { ObservableCollection<Person> personlist = new ObservableCollection<Person>(); Person selectperson = null; // 构造函数 public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { //throw new NotImplementedException(); personlist.Add(new Person() { MyName = "张三", Company = "某某有限公司" }); personlist.Add(new Person() { MyName = "有健", Company = "天津有限公司" }); personlist.Add(new Person() { MyName = "奉沈", Company = "广西有限公司" }); personlist.Add(new Person() { MyName = "李四", Company = "无名有限公司" }); personlist.Add(new Person() { MyName = "王五", Company = "王某有限公司" }); personlist.Add(new Person() { MyName = "小明", Company = "小明有限公司" }); listBox1.ItemsSource = personlist; } private void MenuItem_Click(object sender, RoutedEventArgs e) { MenuItem menu = (MenuItem)sender; string header = (sender as MenuItem).Header.ToString(); ListBoxItem selectlistitem = this.listBox1.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem; if (selectlistitem == null) { return; } //判断选择的是哪一项 if (menu.Header.ToString() == "变色") { selectlistitem.Background = new SolidColorBrush(Colors.Green); } //以下删除操作有一个问题,好像并不能把选中项删除 else if (menu.Header.ToString() == "删除") { selectperson = listBox1.SelectedItem as Person; if (selectperson == null) { MessageBox.Show("请先选择一个选项再进行删除!"); } else { selectperson = (sender as Person); personlist.Remove(selectperson); listBox1.ItemsSource = personlist; } } else if (menu.Header.ToString() == "全部删除") { personlist.Clear(); listBox1.ItemsSource = personlist; } } } //定义联系人信息的类 public class Person { public string MyName { get; set; } public string Company { get; set; } } }