ListboxItem returning "Object reference not set to an instance of an object." on 1st clickHi there,<br/>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.<br/><br/> <pre lang="x-c#">private void txtRewardDescription_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { try { RewardDetail selectedItem = (RewardDetail)lstAvailableRewards.SelectedItem; MessageBox.Show(string.Format(&quot;Selected Reward is - {0}&quot;,selectedItem.RewardDescription)); } catch (Exception ex) { MessageBox.Show(ex.Message); } }</pre> <br/>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.<br/><br/>What am I doing wrong?<br/><br/>Thank you© 2009 Microsoft Corporation. All rights reserved.Fri, 10 Jul 2009 09:06:40 Z906fd543-b7c2-4d75-9ee2-ee0b11e1c27dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/906fd543-b7c2-4d75-9ee2-ee0b11e1c27d#906fd543-b7c2-4d75-9ee2-ee0b11e1c27dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/906fd543-b7c2-4d75-9ee2-ee0b11e1c27d#906fd543-b7c2-4d75-9ee2-ee0b11e1c27dspnzhttp://social.msdn.microsoft.com/Profile/en-US/?user=spnzListboxItem returning "Object reference not set to an instance of an object." on 1st clickHi there,<br/>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.<br/><br/> <pre lang="x-c#">private void txtRewardDescription_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { try { RewardDetail selectedItem = (RewardDetail)lstAvailableRewards.SelectedItem; MessageBox.Show(string.Format(&quot;Selected Reward is - {0}&quot;,selectedItem.RewardDescription)); } catch (Exception ex) { MessageBox.Show(ex.Message); } }</pre> <br/>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.<br/><br/>What am I doing wrong?<br/><br/>Thank youSat, 04 Jul 2009 23:43:48 Z2009-07-04T23:43:48Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/906fd543-b7c2-4d75-9ee2-ee0b11e1c27d#3099ad69-c2fc-49bb-8dda-5677fefa9c4bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/906fd543-b7c2-4d75-9ee2-ee0b11e1c27d#3099ad69-c2fc-49bb-8dda-5677fefa9c4bBruce.Zhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=Bruce.ZhouListboxItem returning "Object reference not set to an instance of an object." on 1st clickHi spnz,<br/> <br/> 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.<br/> <br/>       if(selectedItem!=null)<br/>       {<br/>                 MessageBox.Show(string.Format(&quot;Selected Reward is - {0}&quot;,selectedItem.RewardDescription));<br/>       }<br/> <br/> If there's any problem, please feel free to contact me.<br/> <br/> Best regards,<br/> Bruce Zhou<hr class="sig">Please mark the replies as answers if they help and unmark if they don't.Wed, 08 Jul 2009 07:49:13 Z2009-07-08T07:49:13Z