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 GUI之事件处理
常见事件及相应事件源类型 事件源 用户操作 事件 JButton 点击按钮 ActionEvent JTextField 在文本域按回车键 ActionEvent JCheckBox 点击复选框 Ac ...
- 组播(Multicast)传输
组播(Multicast)传输: 在发送者和每一接收者之间实现点对多点网络连接. 如果一台发送者同时给多个的接收者传输相同的数据,也只需复制一份的相同数据包.它提高了数据传送效率.减少了骨干网络出现拥 ...
- gradle android
从github下载两个开源项目: PagerSlidingTabStrip | Android-Universal-Image-Loader-master https://github.c ...
- flash 读取系统默认编码
java有类可以直接读取,但貌似flash没有. Charset.defaultCharset(); 但是浏览器里可以有. document.defaultCharset;//从当前的区域语言中获取默 ...
- PinYinCls
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...
- C++容器类概述
原文转自:http://blog.csdn.net/wangxiaolong_china/article/details/8362540 什么是容器 首先,我们必须理解一下什么是容器,在C++ 中容器 ...
- Windows消息初探(1)
Windows消息初探(1) 最近恢复对Windows API的学习,深深感受到没有对应的中文资料的痛苦,于是上MSDN上面去将Windows消息搞了一些回来翻译出来,供自己查阅,也与大家分享,能力有 ...
- Skill
Skill Yasser is an Egyptian coach; he will be organizing a training camp in Jordan. At the end of ca ...
- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
转自:http://www.linuxidc.com/Linux/2012-05/60966.htm 在工作中,每次部署应用时都需要从本机Windows服务器拷贝文件到Linux上,有时还将Linux ...
- hibernate使用原生SQL查询返回结果集的处理
今天没事的时候,看到公司框架里有一个用原生SQL写的函数,说实在以前自己也干过这事,但好久都没有用,都忘得差不多了,现在基本都是用的hql语句来查询结果.hibernate中使用createSQLQu ...