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)(一)右键菜单 上一节说到如 ...
随机推荐
- [Java] 使用Comparator排序对象
package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ...
- JDBC中的事务-Transaction
事务-Transaction 某些情况下我们希望对数据库的某一操作要么整体成功,要么整体失败,经典的例子就是支付宝提现.例如我们发起了支付宝到银行卡的100元提现申请,我们希望的结果是支付宝余额减少1 ...
- java类总结
1.java类与类之间存在六种关系:继承,实现,依赖,关联,聚合组成 2.一个类可以继承另外一个类,并在此基础上添加自己的特有的功能. 3.一个类的方法中操作另外一个类的对象,这种情况称为第一个类依赖 ...
- centos6.5网络配置问题:ping不通
遇到的问题:win7网卡ping不通 win7中的VMnet8信息 centos6.5设置: /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ...
- OC基础(14)
Xcode设置 内存管理原则 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 ...
- ibatis cannot find <sql> pattern
"Could not find SQL statement to include with refid xxx"A: 认真把每个用到xxx的地方都看一遍
- SparkSQL使用之Spark SQL CLI
Spark SQL CLI描述 Spark SQL CLI的引入使得在SparkSQL中通过hive metastore就可以直接对hive进行查询更加方便:当前版本中还不能使用Spark SQL C ...
- IEnumerable和List有什么区别?
如下.IList接口可以使用更多的方法.比如你看一个集合是否包含相应实体, IEnumerable不行,而 IList里有Contains,相应的实现了IList的可以添加,删除相应实体.而IEnum ...
- 使用 Cocos2d-x 3.1.1 创建 Windows Phone 8 游戏开发环境
cocos2d-x 是目前流行的游戏游戏开发框架,目前最新的版本是 3.1.1, 网上有些教程已经比较老了,本文将会介绍如何使用最新的 3.1.1 创建 Windows Phone 8 开发环境. 本 ...
- HDU1116图论
http://acm.split.hdu.edu.cn/showproblem.php?pid=1116 #include<stdio.h> #include<algorithm&g ...