Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button)
在很多使用DataBound的ListBox案例中,我们都监听了它的 SelectionChanged 事件,当我们用手指点击某一项时,可以从 ListBox.SelectedItem 属性上很容易获得这个被点击的对象。然而,万一你的ListBox的单项里面有很多类似于 Button, TextBlock 这样的控件,而你刚好又要捕获这些控件的点击事件时,那你该这么做?不过我将在下面的文章中谈谈一个简单的解决方法
public class Person {public Person(string firstName, string lastName, int age){FirstName = firstName;LastName = lastName;Age = age;}public string FirstName { get; set; }public string LastName { get; set; }public int Age { get; set; }public override string ToString(){return LastName + ", " + FirstName + "{" + Age + "}";} }
接着来写XAML布局代码,先添加一个3列的ListBox,如下代码所示。
<phone:PhoneApplicationPage x:Class="DataboundMultiListBoxSelection.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"FontFamily="{StaticResource PhoneFontFamilyNormal}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"shell:SystemTray.IsVisible="True"><!--LayoutRoot is the root grid where all page content is placed--><Grid x:Name="LayoutRoot" Background="Transparent"><ListBox x:Name="PeopleListBox" SelectionChanged="PeopleListBox_SelectionChanged"><ListBox.ItemTemplate><DataTemplate><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="150" /><ColumnDefinition Width="150" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions><Button Grid.Column="0" Content="{Binding FirstName}" Click="FirstNameButton_Click" /><TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{Binding LastName}" MouseLeftButtonUp="LastNameTextBlock_MouseLeftButtonUp" /><Rectangle Grid.Column="2" Width="36" Height="36" Fill="Red" MouseLeftButtonUp="AgeRect_MouseLeftButtonUp" /></Grid></DataTemplate></ListBox.ItemTemplate></ListBox></Grid> </phone:PhoneApplicationPage>
然后在C#后台代码中,我们在Page_Load事件中为ListBox添加数据。
public partial class MainPage : PhoneApplicationPage {// Constructorpublic MainPage(){InitializeComponent();Loaded += new RoutedEventHandler(MainPage_Loaded);}void MainPage_Loaded(object sender, RoutedEventArgs e){List<Person> people = new List<Person>();people.Add(new Person("Tim", "Mgee", 36));people.Add(new Person("Frank", "Solo", 77));people.Add(new Person("Hanna", "Jones", 77));PeopleListBox.ItemsSource = people;}
运行程序,结果如下图所示:
接着。为Button实现点击事件的处理:
{Person selectedPerson = ((sender as Button).DataContext as Person);MessageBox.Show("First Name Button clicked: " + selectedPerson.FirstName); }
比较有意思的是我们这里首先将 sender 强制转换成 Button, 然后获取 Button.DataContext 属性。问题来了,什么事 DataContext? 当你将数据绑定到ListBox时,每一项被分配一个 DataContext 数据来表示绑定的单项数据。在今天这个范例中,DataContext对应的是每一个Person。而且DataContext我们可以称为路由属性,在ListBoxItem的每一个子控件中都可以被获取。所以这也是为什么Button.DataContext就是我们所需要的Person的原因。
private void LastNameTextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {Person selectedPerson = ((sender as TextBlock).DataContext as Person);MessageBox.Show("Last Name TextBlock clicked: " + selectedPerson.LastName);e.Handled = true; }private void AgeRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {Person selectedPerson = ((sender as Rectangle).DataContext as Person);MessageBox.Show("Age Rectangle clicked: " + selectedPerson.Age);e.Handled = true; }
注意我们这里设置 Handled 属性为true,这一点与Button的点击事件处理可不一样,因为 MouseLeftButtonUp 事件如果不手动停止的话,会无限路由下去。所以当然要设置 Handled = true 来标记该事件已经完成。
最后,我们看看ListBox.SelectionChanged事件的做法。这里的处理是捕获所有的没有点击到按钮,TextBlock以及Rectangle的点击事件。
private void PeopleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {if (PeopleListBox.SelectedIndex == -1)return;Person selectedPerson = ((sender as ListBox).SelectedItem as Person);MessageBox.Show("ListBox selected: " + selectedPerson);PeopleListBox.SelectedIndex = -1; }
注意上面代码一开始去检测 SelectedIndex 是否为 -1 ,而且最后还强行设置 SelectedIndex = -1,如果你不这样做,那么SelectionChanged事件在你触碰到ListBox后将不会按照顺序被触发。即Selection不会被改变。那么我们将看到下图这个样子。
转自:http://www.oschina.net/question/213217_52095
Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button)相关推荐
- DICOM:再次剖析fo-dicom中DicomService的自定义事件绑定
题记: 趁着<从0到1>大火的热潮,近期重新翻阅了一遍<从一到无穷大>(这样是不是感觉整个非负数轴就圆满了^_^).虽然作为科普类书籍,但是里面的内容还是比较深奥,幸亏有作者精 ...
- magix中的2种事件绑定方式
可操作html与js magix采用html与js文件分离的写法,当我们在阅读时,希望能直观的看出节点绑定了哪些事件或这个函数是为哪种事件服务的. 如下html <div><butt ...
- $.ligerdialog.open中确定按钮加事件_彻底搞懂JavaScript中的this指向问题
JavaScript中的this是让很多开发者头疼的地方,而this关键字又是一个非常重要的语法点.毫不夸张地说,不理解它的含义,大部分开发任务都无法完成. 想要理解this,你可以先记住以下两点: ...
- 如何在windows server 2008 AD中禁用USB端口(转庐阳侠们博客)
本文使用的系统:Windows Server 2008 R2 企业版. 域功能级别:Windows Server 2008 R2 在Windows Server 2008 AD中的组策略编辑器,依次找 ...
- Windows Mobile 5.0 中为开发人员提供的新功能(3)
Windows Mobile 5.0 中为开发人员提供的新功能(3) Pocket Outlook 增强功能 Pocket Outlook 向用户和应用程序开发人员提供了易于使用的 PIM.将 Poc ...
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...
- Windows Server 2008 R2中的Hyper-V
现在虚拟化技术已经成为数据中心的一个主要组成部分.虚拟化技术提供的操作效率帮助企业机构大幅简化操作流程和降低能耗. Windows Server 2008提供以下类型的虚拟化: ?Hyper-V提供的 ...
- Windows Azure 虚拟网络中虚拟机的网络隔离选项
最近我们发布了一份<Windows网络安全白皮书>(单击此处下载),文中深入说明了客户可以如何利用该平台的本地功能,为他们的信息资产提供最好的保护. 由首席顾问Walter Myer ...
- Windows Mobile 6 SDK 中的 GPS 工具
Windows Mobile 6 SDK 中的 GPS 工具 GPS Intermediate Driver 自从 Windows Mobile 5.0 引进了 GPS Intermediate Dr ...
最新文章
- C# 实现对接电信交费易自动缴费 续(winio/winring0 自动填密码)
- 《转》四本与携程相关的书
- oracle adg switch over,Oracle11gr2_ADG管理之switchover补充
- java需要了解的几个算法
- Java微信授权登陆
- 联想笔记本键盘排线_联想笔记本排线
- 数据结构c语言版ppt答案,《数据结构(C语言版)》习题答案.ppt
- Java服务优雅停机_微服务架构—优雅停机方案
- 基于spring+spring mvc+mybatis 开发 JavaWeb 医院人事管理系统(Maven项目)
- 直角三角形面积Java_利用Eclipse编写第一个简单的Java程序,实现如下功能。 键盘输入:直角三角形两直角边a, b,根据勾股定理计算斜边的平方值。并输出。 例如:运行效果如下...
- Excel图表5——旋风图(对称条形图)
- xmind可以画流程图吗_怎样用XMind方便地制作流程图
- (亲测很实用)地理位置批量转经纬度,基于百度地图api
- excel快速拆分数据的方法
- 格鲁吉亚理工学院 计算机难度,去美国读CS和工程专业的同学请注意,这里有50所性价比最高的学校待查收!...
- 考研英语作文 万能的开头结尾
- 免费python基础笔记_python的基础练习笔记
- 移动智能终端安全技术要求及测试评价方法
- unigui设置文件下载类型
- 架构师的软实力之技术创新、企业执行