惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
The Cloudflare Blog
T
Tailwind CSS Blog
H
Help Net Security
腾讯CDC
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
C
Check Point Blog
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 黎波

精至手机药典Windows Phone 7版 [WP7]修改 Pivot 控件的 PivotItem 标题字体大小 [WP7]CheckBox 文字自动换行 Windows Phone 7 系统主题颜色RGB和Hex值 [WP7]MD5加密字符串 [iOS]MD5加密字符串 [Android]MD5加密字符串 [Android]获取设备IP地址 如何检查对象的类型[iOS/Android/Windows Phone] 精至手机药典iPhone版 [iOS]NSString常用代码 目标身高iPhone版 目标身高Android版 [iOS]让Xcode 4.2生成的app支持老的iOS设备(armv6) [iOS]升级到 Xcode 4.1 后 sqlite3.h 引起 Expected declaration specifiers before 'interface' 错误 [转载]用手机玩转汽车 体验安吉星手机应用程序 [iOS]Xcode 4.1 bug: Text Field 引起 EXC_BAD_ACCESS 错误的解决 精至手机药典Android版 [Android]使用ProGuard遇到“conversion to Dalvik format failed with error 1”错误的解决办法
[WP7]关于 ListBox 的 SelectionChanged 事件
黎波 · 2012-06-25 · via 博客园 - 黎波

问题描述:在使用 ListBox 的 SelectionChanged 事件时,可能会遇到一个小问题,就是当重复选中同一个 Item 时,SelectionChanged 事件只是在第一次选中 Item 时触发,我们期望的是每次都能触发。

原因分析:原因很简单,ListBox 列表框里面的 Item 被选中后,ListBox 的 SelectedIndex 会被设置为该 Item 的 Index,当第二次选中这个 Item 时,事实上 SelectedIndex 并没有变,因此 SelectionChanged 事件也不会被触发,从事件的名称上理解也应该如此。

解决思路:为了达到我们期望的效果,只需要在 SelectionChanged 事件处理方法中将 ListBox.SelectedIndex 设置为 -1,即没有选中任何 Item。

示例代码: 

private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{  
    // If selected index is -1 (no selection) do nothing  
    if (listBox.SelectedIndex == -1)  
        return;  
 
    // Navigate to the new page  // Reset selected index to -1 (no selection)  
    listBox.SelectedIndex = -1;              
}

参考:http://forums.create.msdn.com/forums/p/66377/406079.aspx#406079

作者:黎波
博客:http://bobli.cnblogs.com/
日期:2012年6月25日