c# - How to select the listbox items from View Model -
i want select list box items view model?
my xaml is:-
<grid x:name="mvvmlistboxuicontainer" grid.row="1" margin="2,0,2,0"> <listbox height="374" itemssource="{binding studentdetails,mode=twoway}" horizontalalignment="left" margin="2,6,0,0" name="listbox1" verticalalignment="top" width="476" borderbrush="#00410d0d"> <i:interaction.triggers> <i:eventtrigger eventname="tap"> <i:invokecommandaction command="{binding eventpagecommand, mode=oneway}"/> </i:eventtrigger> </i:interaction.triggers> <listbox.itemtemplate> <datatemplate> <border borderbrush="gray" padding="5" borderthickness="1"> <stackpanel orientation="horizontal"> <border borderbrush="wheat" borderthickness="1"> <image name="listpersonimage" source="{binding personimage}" height="100" width="100" stretch="uniform" margin="10,0,0,0"/> </border> <textblock text="{binding firstname}" name="firstname" width="200" foreground="white" margin="10,10,0,0" fontweight="semibold" fontsize="22" /> <button margin="-100,0,0,0" height="80" width="80" datacontext="{binding datacontext, elementname=listbox1}" command="{binding addperson}"> <button.background> <imagebrush imagesource="{binding addimage}" stretch="fill" /> </button.background> </button> </stackpanel> </border> </datatemplate> </listbox.itemtemplate> </listbox>
my view model:-
eventpagecommand = new reactiveasynccommand(); eventpagecommand.subscribe(x => { messagebox.show("list box item clicked."); });
here when click on list box items message box showing. can not show selected items. can show xaml.cs.
private void listbox1_tap(object sender, system.windows.input.gestureeventargs e) { listbox listbox = (listbox)sender; listboxeventsmodel items = (listboxeventsmodel)listbox.selecteditem; messagebox.show("selected name"+items.firstname); //if(!constants.buttonselected) //messagebox.show("list item selected..!!"); }
but when try view model can not show selected items. please let me know how show selected items. have try this:-
eventpagecommand = new reactiveasynccommand(); eventpagecommand.subscribe(x => { messagebox.show("list box item clicked."); listbox listbox = (listbox)sender; listboxeventsmodel items = (listboxeventsmodel)listbox.selecteditem; messagebox.show("selected name" + items.firstname); });
but receiving error:- the name 'sender' not exist in current context
in xaml in listbox:
selecteditem={binding selectedstudent, mode=twoway}
then in viewmodel:
public observablecollection<your_object_here> selecteditems { get; set;}
edit full example:
<listbox margin="5,0" grid.row="1" grid.rowspan="2" itemssource="{binding roles}" selecteditem="{binding selecteditem, mode=twoway}"> <listbox.itemtemplate> <datatemplate> <textblock text="{binding path=title}" /> </datatemplate> </listbox.itemtemplate> </listbox>
viewmodel
public class rolesviewmodel : baseviewmodel { private collection<bo.rolebo> roles; private bo.rolebo selecteditem; public bo.rolebo selecteditem { { return selecteditem; } set { selecteditem = value; notifyonpropertychanged("selecteditem"); } } public collection<bo.rolebo> roles { { return roles; } set { roles = value; notifyonpropertychanged("roles"); } } }
Comments
Post a Comment