c# - How to get the currently selected ListView object item back to the ViewModel on a button click -
the title pretty much sums up. i've been trying utilize button's commandparameter i'm not sure should , additional code need in viewmodel class. help appreciated.
xaml:
<itemscontrol> <stackpanel orientation="horizontal"> <button command="{binding buttonclick}" width="150" margin="5" height="22" horizontalalignment="left">click</button> </stackpanel> <listview name="listview" grid.row="1" borderthickness="0" itemssource="{binding myobjects}"> <listview.view> <gridview> <gridviewcolumn header="space id" displaymemberbinding="{binding id}" width="auto" /> </gridview> </listview.view> </listview> </itemscontrol>
viewmodel c#:
public icommand buttonclick { { homecoming new delegatecommand(btnclick); } } private void btnclick() { //access selected object of type myobject here. }
delegatecommand class:
public class delegatecommand : icommand { private readonly action _action; public delegatecommand(action action) { _action = action; } public void execute(object parameter) { _action(); } public bool canexecute(object parameter) { homecoming true; } }
you can bind listview's selecteditem property of viewmodel. way bind 'myobjects' itemssource.
c# wpf mvvm
No comments:
Post a Comment