[WP7]關於 ListBox 的 SelectionChanged 事件

 

問題描述:在使用 ListBox 的 SelectionChanged 事件時,可能會遇到一個小問題,就是當重複選中同一個 Item 時,SelectionChanged 事件只是在第一次選中 Item 時觸發,咱們指望的是每次都能觸發。web


緣由分析:緣由很簡單,ListBox 列表框裏面的 Item 被選中後,ListBox 的 SelectedIndex 會被設置爲該 Item 的 Index,當第二次選中這個 Item 時,事實上 SelectedIndex 並無變,所以 SelectionChanged 事件也不會被觸發,從事件的名稱上理解也應該如此。ide


解決思路:爲了達到咱們指望的效果,只須要在 SelectionChanged 事件處理方法中將 ListBox.SelectedIndex 設置爲 -1,即沒有選中任何 Item。spa


示例代碼: code

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#406079blog

相關文章
相關標籤/搜索