views:

30

answers:

1

I have a ListBox with dates.
Each ListBoxItem (date) have another 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?

A: 

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;
Frexuz