1. 新建一个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;
}
}
}

windows phone上下文菜单ContextMenu的使用示例的更多相关文章

  1. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

  2. Android菜单详解(四)——使用上下文菜单ContextMenu

    之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...

  3. Android 菜单 之 上下文菜单ContextMenu

    所谓上下文菜单就是当我们长按某一个文件时弹出的菜单 操作这个菜单我们要重写onCreateContextMenu()方法 如上一篇文章一样,对于这个菜单中选型的操作也有动态添加和xml文件添加两种方法 ...

  4. Android 上下文菜单 ContextMenu

    public class MainActivity extends Activity { private ListView listView; @Override protected void onC ...

  5. Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)

    菜单的概念,现在已经很普及了.Windows系统.Mac.桌面版Linux.Java Swing等,都有可视化菜单.一.Android平台3种菜单  选项菜单(OptionMenu).上下文菜单(Co ...

  6. 如何添加“在这里打开PowerShell”到Windows中的上下文菜单

    It was only a matter of time, right? Due to my recent infatuation passionate love affair with PowerS ...

  7. (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

    原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...

  8. 与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成

    原文:与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单"应用程序..."和"共享..." ...

  9. (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

    (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 外壳编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 上一节说到如 ...

随机推荐

  1. Informix如何释放异常的锁资源

    问题 在Informix数据库中,锁的使用和释放是自动完成的.但在某些异常情况下,当前台程序退出(正常或异常)后,相应在数据库中的会话没有终止,其占有的资源(主要是锁)没有被释放,影响了其他用户的使用 ...

  2. QT学习之路---信号槽

    #include<QApplication> #include<QPushButton> int main(int argc,char *argv[]) { QApplicat ...

  3. 获取当前访问的url

    1.获取完全url,包含参数: request.getRequestURL(); 2.获取部分: request,getRequestURI 不包含参数,协议名称 获取访问的参数: request.g ...

  4. Eclipse自定义Ant版本

    changed the ANT_HOME in the Windows>Preferences>Ant>Runtime>Classpath>Ant Home>浏览文 ...

  5. Git简单使用

    删除本地旧分支,拉取新分支 #!/bin/bash set -e cd /project/ git pull git fetch ori --prune git branch -r|sed 's/or ...

  6. 06-CABasicAnimation基础核心动画

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. 制作chm无搜索标签解决方法

    chm无搜索标签解决方法: 1.hpp文件中设置Full-text search=Yes 2.下面[windows]有个数字设置为0x420, 网上普通流传的是0x20 === chm制作一般要三个文 ...

  8. jQuery中 wrap() wrapAll() 与 wrapInner()的区别

    今晚看书的时候发现jQuery有三个包裹节点的方法,百度了一下jQuery wrap() / wrapAll() / wrapInner(),果然搜索结果 W3School的文档说明是排第一的. 可是 ...

  9. Hibernate 常见异常

    Hibernate 常见异常net.sf.hibernate.MappingException        当出现net.sf.hibernate.MappingException: Error r ...

  10. startup毕业论文

    今天起得相对比较晚,为的是一个没有目的面试,去了的结果.只是打击一下自己的自信心,走的时候,面试官冷冷的说了一句,你的面试到此结束,是的,我并没有很伤心,在门外等面试的时候,我就非常的后悔,今天是不该 ...