c# - System.NullReferenceException While trying to set TextBox value -


i have combobox in code, trying value of selected item , write in textblock. while running code exception of type 'system.nullreferenceexception'. doing wrong?this design

 <stackpanel orientation="horizontal" >            <combobox selectionchanged="combobox_selectionchanged">             <comboboxitem content="first" isselected="true"/>             <comboboxitem content="second"/>             <comboboxitem content="third"/>         </combobox>         <textblock name="comboresult"  />     </stackpanel> 

and code

private void combobox_selectionchanged(object sender, selectionchangedeventargs e)     {         combobox cb=(combobox)sender;         comboboxitem cbi = (comboboxitem)cb.selecteditem;         comboresult.text = cbi.content.tostring();     } 

remove isselected property comboboxitem , works. can set combobox.selectedindex after initialization. i'm not familiar how xaml renders, think it's triggering selectionchanged event before textblock renders. can observed using grid columns rather stackpanel, , putting textblock first in xaml (but in higher column combobox); works fine. additionally, if catch textblock's loaded event, can see selectionchanged event being hit first in original code.


Comments