2010-10-12 78 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;