Windows Phone 7 - 在嵌套的ListBoxes中取消选择ListBoxItem

时间:2010-10-12 15:58:42

标签: wpf wpf-controls windows-phone-7

我有一个带日期的ListBox 每个ListBoxItem(日期)都有另一个ListBox,其中包含该日期的事件。

当我选择一个事件时,它会突出显示(SelectedIndex / SelectedItem),然后我导航到另一个Pivot。这很好。

我的问题是每个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
    }

var episode 第二次为空。怎么样?

1 个答案:

答案 0 :(得分: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;
相关问题