2010-10-12 68 views
0

我有一個帶日期的列表框。
每個ListBoxItem(日期)都有另一個ListBox與該日期的事件。Windows Phone 7 - 取消選擇嵌套列表框中的ListBoxItem

當我選擇一個事件時,它被突出顯示(SelectedIndex/SelectedItem),我導航到另一個樞軸。這工作正常。

我的問題是,每個ListBox都有它自己的SelectedItem。我想清除每個ListBox中的SelectedItem,但是我無法使它工作!

這裏是我的嘗試:

//Store a reference to the latest selected ListBox 
    public ListBox SelectedListBox { get; set; } 

    private void SelectionChangedHandler(object sender, SelectionChangedEventArgs e) 
    { 
     ListBox lstBox = ((ListBox)sender); 

     //This row breaks the SECOND time!! 
     var episode = (Episode)lstBox.SelectedItem; 

     episodeShowName.Text = episode.Show; //Do some code 
     episodeTitle.Text = episode.Name; //Do some code 
     episodeNumber.Text = episode.Number; //Do some code 
     episodeSummary.Text = episode.Summary; //Do some code 

     resetListBox(lstBox); //Do the reset ! 

     pivot1.SelectedIndex = 1; 
    } 


    private void resetListBox(ListBox lstBox) 
    { 
     if (SelectedListBox != null) 
      SelectedListBox.SelectedIndex = -1; 

     //If I remove this line, the code doesn't break anymore 
     SelectedListBox = lstBox; //Set the current ListBox as reference 
    } 

變種發作是空的第二次。怎麼來的?

+0

快速瀏覽,你不應該被重新傳遞到resetListBox方法列表框的信息?即如果(lstBox!= null)lstBox.SelectedIndex = -1;' – keyboardP 2010-10-12 16:43:46

+0

是不是lstBox我剛選擇的列表框? – Frexuz 2010-10-12 17:07:14

+0

這是,但是爲什麼在將ListBox分配給SelectedListBox之前,您重置了SelectedListBox?我可能在工作流程中缺少某些東西,所以它不一定是錯誤的,但似乎您將重置之前分配的ListBox而不是當前的(直到選擇被再次更改)。當你說代碼不再中斷時,它是否能夠正常工作? – keyboardP 2010-10-12 18:15:44

回答

1

我發現問題了!

private void resetListBox(ListBox lstBox) 
{ 
    if (SelectedListBox != null) 
     SelectedListBox.SelectedIndex = -1; 

    //If I remove this line, the code doesn't break anymore 
    SelectedListBox = lstBox; //Set the current ListBox as reference 
} 

當我設置先前選擇的ListBox的的SelectedIndex爲-1,則SelectionChangedHandler事件被再次觸發(當然)和螺絲了! :d

簡單的解決辦法:

private void SelectionChangedHandler(object sender, SelectionChangedEventArgs e) 
    { 
     ListBox lstBox = ((ListBox)sender); 
     if (lstBox.SelectedIndex < 0) 
      return; 
相關問題