wpf - How to get Listbox selected index? -
i have listbox filled buttons. when buttons clicked new page shows. wanna have selected index of listbox when click on button. listboxitem never selected. there way index button lies clicking button. help.
example code
<listbox> <listbox.itemtemplate> <datatemplate> <button click="viewdetail_click" content="{binding path=area}"></button> </datatemplate> </listbox.itemtemplate> </listbox>
the data context of button contain source data item corresponding list item. you've not shown itemssource
list, can't show code you'd need. this:
observablecollection<mydataitem> items = new observablecollection<mydataitem>(); ... mylist.itemssource = items; ... private void viewdetail_click(object sender, routedeventargs e) { button b = (button) sender; mydataitem detailitem = (mydataitem) b.datacontext; int itemindex = items.indexof(detailitem); }
of course, if reason you're asking index in first place because want hold of corresponding data item, don't need index @ - item's right there in data context.
by way, wouldn't use listbox
here. main thing listbox
brings party selection, putting buttons in items, you're defeating - you've discovered, button handling mouse input before list box has chance to, meaning can't select items. @ point, why have listbox
? base itemscontrol
makes more sense. (wrapped in scrollviewer
if need scrolling.)
Comments
Post a Comment