c# - WP7 - How to get the list box items from ViewModel? -
i trying selected name list box when click list box button.
my xaml page:-
<listbox name="mylistbox" height="733" itemssource="{binding studentdetails,mode=twoway}" horizontalalignment="left" margin="0,35,0,0" verticalalignment="top" width="476"> <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" /> <textblock text="{binding lastname}" name="lastname" width="200" foreground="white" margin="-200,50,0,0" fontweight="semibold" fontsize="22" /> <textblock text="{binding age}" name="age" width="200" foreground="white" margin="10,10,0,0" fontweight="semibold" fontsize="22" /> <button command="{binding buttonclick}" datacontext="{binding datacontext, elementname=mylistbox}" margin="-200,0,0,0" height="80" width="80"> <button.background> <imagebrush stretch="fill" > <imagebrush.imagesource> <bitmapimage urisource="/newexample;component/images/icon_increase.png" /> </imagebrush.imagesource> </imagebrush> </button.background> </button> </stackpanel> </border> </datatemplate> </listbox.itemtemplate> </listbox>
xaml.cs:-
private void button_click(object sender, routedeventargs e) { button mybutton = sender button; listboxwithbuttonmodel dataobject = mybutton.datacontext listboxwithbuttonmodel; int index = mylistbox.items.indexof(dataobject); messagebox.show("button clicked" + dataobject.firstname); }
here can selected name when click button. same thing want view model. please help me selected name viewmodel.
i have seek this.
listbutton = new reactiveasynccommand(); listbutton.subscribe(x => { listboxwithbuttonmodel dataobject = listbutton.datacontext listboxwithbuttonmodel; //int index = mylistbox.items.indexof(dataobject); messagebox.show("button clicked" + dataobject.firstname); });
but here getting error datacontext not contain reactiveasyccommand. please help me solve problem.
my viewmodel:-
public reactiveasynccommand buttonclick { get; set; } public listboxwithbuttonviewmodel() { buttonclick = new reactiveasynccommand(); buttonclick.subscribe(x => { messagebox.show("test"); }); }
herer can show message box. how selected item here??
my try.
public relaycommand<listboxwithbuttonmodel> itemselectedcommand { get; private set; } public listboxwithbuttonviewmodel() { itemselectedcommand = new relaycommand<listboxwithbuttonmodel>(itemselected); } private void itemselected(listboxwithbuttonmodel myitem) { messagebox.show("testing"); if (null != myitem) messagebox.show("name==>" + myitem.firstname); }
here can not selected item. please give me thought resolve problem.
it isn't stated suspect problem myitem
parameter null
because never pass parameter in xaml.
use commandparameter
property pass parameter command
, illustration :
<button command="{binding datacontext.itemselectedcommand, elementname=mylistbox}" commandparameter="{binding}" margin="-200,0,0,0" height="80" width="80"> <button.background> <imagebrush stretch="fill" > <imagebrush.imagesource> <bitmapimage urisource="/newexample;component/images/icon_increase.png" /> </imagebrush.imagesource> </imagebrush> </button.background> </button>
c# xaml windows-phone-7 mvvm listbox
No comments:
Post a Comment