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;
}
}
}
windows phone上下文菜单ContextMenu的使用示例的更多相关文章
- Android进阶(二十八)上下文菜单ContextMenu使用案例
上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...
- Android菜单详解(四)——使用上下文菜单ContextMenu
之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...
- Android 菜单 之 上下文菜单ContextMenu
所谓上下文菜单就是当我们长按某一个文件时弹出的菜单 操作这个菜单我们要重写onCreateContextMenu()方法 如上一篇文章一样,对于这个菜单中选型的操作也有动态添加和xml文件添加两种方法 ...
- Android 上下文菜单 ContextMenu
public class MainActivity extends Activity { private ListView listView; @Override protected void onC ...
- Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
菜单的概念,现在已经很普及了.Windows系统.Mac.桌面版Linux.Java Swing等,都有可视化菜单.一.Android平台3种菜单 选项菜单(OptionMenu).上下文菜单(Co ...
- 如何添加“在这里打开PowerShell”到Windows中的上下文菜单
It was only a matter of time, right? Due to my recent infatuation passionate love affair with PowerS ...
- (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...
- 与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成
原文:与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单"应用程序..."和"共享..." ...
- (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
(本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 外壳编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 上一节说到如 ...
随机推荐
- php读取数据库数据,出现中文乱码(数据库中没有出现乱码)
添加header(“content-type: text/html; charset=utf-8”) php header() 函数向客户端发送原始的 HTTP 报头, 认识到一点很重要,即必须在任何 ...
- [ActionScript 3.0] AS3调用百度地图API
package { import baidu.map.basetype.LngLat; import baidu.map.basetype.Size; import baidu.map.config. ...
- 修改hive内存限制
错误: GB physical memory used; 4.7 GB of 2.1 GB virtual memory used. Killing container. Dump of the pr ...
- git小操作之checkout、stash
git checkout会带上当前changed但没有commit的内容到目标分支 git stash用来暂存当前改动,并且会退代码到上一个commit:git stash pop则取出所stash的 ...
- HDU1213
http://acm.split.hdu.edu.cn/showproblem.php?pid=1213 #include<stdio.h> #include<algorithm&g ...
- Xcode 中 Git 的配置与使用
Xcode 中 Git 的配置与使用主要围绕下面几个问题展开阐述: 问题1,如何在Xcode中创建本地代码库,并添加和提交代码到本地代码库? 问题2,如何在Xcode中提交推送给远程服务器代码库? 问 ...
- DOJO官方API翻译或解读-dojo/store (自定制存储器)
dojo/store 是对已存数据的访问和存储的统一接口,dojo/store意图以一个简单.易于使用和扩展的API来,替代.集合和改善 dojo/data 和dojox/storage .基于HTM ...
- Oozie JMS通知消息实现--根据作业ID来过滤消息
一,介绍 本文使用Oozie的消息通知功能,并根据JMS规范中的消息选择器(Selector)实现 根据作业的ID来过滤消息. 首先搭建好JMS Provider(ActiveMQ) ,并进行相关配置 ...
- Oracle密码过期the password has expired
oracle11G新特征,半年会提醒一次修改新密码 正常情况:只要输入原先帐号密码,回车即可. 不正常情况:输入新密码帐号,回车,提示验证错误,这时就需要切换到没过期的管理员用户,高权限也行,输入 ...
- MVC中使用过滤器记录异常日志
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Filte ...