wpf - c# - how to handle with null selectedItem? -


i have issue selecteditem of listbox. when select item of listbox, popup displayed click add button select image (it contains value of selecteditem) working fine. after clicking add button select image, realise image wrong, click add button again select image, started problem because selecteditem null. how handle it? how stay value of selecteditem? given code appreciated.

if (lstdinner.selecteditem != null) {   output = _imageinserter.insertimage(imagename, lstdinner.selecteditem.tostring());   popuptoysimage.isopen = true;   strdinner.dinnersdetails = lstdinner.selecteditem.tostring()  }  else {  // strdinner.dinnersdetails = null cause problem.  output = _imageinserter.insertimage(imagename, strdinner.dinnersdetails);  popupdinnerimage.isopen = true;  } 

update here:

wpf:

<listbox style="{dynamicresource listboxstyle1}"  displaymemberpath="dinner" borderbrush="#fff0f0f0"  x:name="lstdinner" fontsize="20" horizontalalignment="left" margin="0,110,0,72.667" width="436" selectionmode="extended"  previewmouseleftbuttondown="mousedownhandler"  scrollviewer.cancontentscroll="true" uselayoutrounding="false" keydown="lstdinner_keydown" mousedoubleclick="lstdinner_mousedoubleclick" > 

events in c#:

    private void mousedownhandler(object sender, mousebuttoneventargs e)     {         var parent = (listbox)sender;          _dragsource = parent;          var data = getobjectdatafrompoint(parent, e.getposition(parent));          if (e.changedbutton == mousebutton.left && e.clickcount == 1)         {             if (data != null)                 dragdrop.dodragdrop(parent, data, dragdropeffects.move);         }     }       private void lstdinner_keydown(object sender, keyeventargs e)     {         if (e.key == key.delete)         {             removeitemsfromdatabase();         }     }          private void lstdinner_mousedoubleclick(object sender, mousebuttoneventargs e)     {         _dinnerimage = new dinnerimageextractor();         bitmapimage getimage = new bitmapimage();          if (lstdinner.selecteditem != null)         {             getimage = _dinnerimage.getdinnerimages(lstdinner.selecteditem.tostring());              if (getimage != null)             {                 dinnerimagepopup.source = getimage;             }             else             {                 dinnerimagepopup.source = new bitmapimage(new uri("/dinnerapplicationwpf;component/menu/images/noimage-icon-pink.png", urikind.relative));              }              popupdinnerimage.isopen = true;           //  popupinstrcution.isopen = false;          }     } 

i suggest this

if ( lstdinner.selecteditem == null) {   output = _imageinserter.insertimage(imagename, lsttoys.selecteditem.tostring());   popuptoysimage.isopen = true;   lstdinner.databind(); } 

note: may not work dont have actual code. have added databind() in if statement, if selected item null. should refresh list.


Comments