Microsoft Developer Network > Página Inicial dos Fóruns > Windows Presentation Foundation (WPF) > ListboxItem returning "Object reference not set to an instance of an object." on 1st click
Fazer uma PerguntaFazer uma Pergunta
 

RespondidoListboxItem returning "Object reference not set to an instance of an object." on 1st click

  • sábado, 4 de julho de 2009 23:43spnz Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    Hi there,
    I have created a listbox inside my data template I have a textblock with the LeftMouseButtonDown event hooked into it. I have a basic little event like this.

    private void txtRewardDescription_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                try
                {
                    RewardDetail selectedItem = (RewardDetail)lstAvailableRewards.SelectedItem;
                    MessageBox.Show(string.Format("Selected Reward is - {0}",selectedItem.RewardDescription));
               
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
                
            }
    

    Now my problem is the 1st time I click on an item within the list I get the Object reference error however if i press it again I get the details I was expecting.

    What am I doing wrong?

    Thank you

Respostas

  • quarta-feira, 8 de julho de 2009 7:49Bruce.ZhouMSFT, ModeradorMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    Hi spnz,

    Please make sure there is an item selected in the ListBox before you click the TextBox element. Otherwise the selected item can be null. If you want to prevent this exception, you can check if the selectedItem is null before you decide showing the MessgeBox.

          if(selectedItem!=null)
          {
                    MessageBox.Show(string.Format("Selected Reward is - {0}",selectedItem.RewardDescription));
          }

    If there's any problem, please feel free to contact me.

    Best regards,
    Bruce Zhou
    Please mark the replies as answers if they help and unmark if they don't.

Todas as Respostas

  • quarta-feira, 8 de julho de 2009 7:49Bruce.ZhouMSFT, ModeradorMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    Hi spnz,

    Please make sure there is an item selected in the ListBox before you click the TextBox element. Otherwise the selected item can be null. If you want to prevent this exception, you can check if the selectedItem is null before you decide showing the MessgeBox.

          if(selectedItem!=null)
          {
                    MessageBox.Show(string.Format("Selected Reward is - {0}",selectedItem.RewardDescription));
          }

    If there's any problem, please feel free to contact me.

    Best regards,
    Bruce Zhou
    Please mark the replies as answers if they help and unmark if they don't.