开发者

Windows Phone 7 - Deselecting ListBoxItem in nested ListBoxes

I have a ListBox with dates.

Each ListBoxItem (date) have ano开发者_C百科ther ListBox with that date's events.

When I select an event it gets highlighted (SelectedIndex/SelectedItem) and I navigate to another Pivot. This works fine.

My problem is that every ListBox has it's own SelectedItem. I want to clear the SelectedItem from each ListBox, but I cannot get it to work!

Here's my try:

    //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 is null the second time. How come?


I found the problem!

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
}

When I set the previous selected ListBox's SelectedIndex to -1, the SelectionChangedHandler event gets triggered again (of course) and screws up ! :D

Easy fix:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜