c# - When using ListBox in WPF, how to get access to the selected value(s) programmatically -
in code i'm using list box display objects class i'm creating. want able click on item in list box , programmatically use selected item. items come dictionary shown below.
private dictionary<int32, myclass> collection; public window1() { listbox1.selectionchanged += new selectionchangedeventhandler(clickanitem); listbox1.itemsource = collection; }
now, works , listbox displays collection expect, , have event firing should, i'm stuck on how use selected value.
private void clickanitem(object sender, routedeventargs e) { listbox list = sender listbox; /** list has int32 , myclass object can't seem * them out of there programmatically */ }
i've tried casting listbox.selecteditems object of type dictionary no avail.
i didn't run it, here question seems similar. however, stay away editing xaml if possible. more can @ run-time better.
so question is, how access 'int32' , 'myclass' of selected item? i've used c# in past i'm jumping , has been bugging me on hour.
you need value selecteditem
property on listbox , cast appropriate type. in case keyvaluepair<int32, myclass>
makes dictionary
give try:
private void clickanitem(object sender, routedeventargs e) { listbox list = sender listbox; var selecteditem = listbox.selecteditem keyvaluepair<int32, myclass>; }
Comments
Post a Comment