c# - Reacting mouse click event in viewModel instead of code behind -
i have wpf app list view set via viewmodel.
i have code below handle mouse click on list view have read iot better handle in view model instead of code behind?
i have code below
xaml:
<listview ... previewmouseleftbuttonup="listview_click"> ...
code behind:
private void listview_click(object sender, routedeventargs e) { var item = (sender listview).selecteditem; if (item != null) { ... } }
i tried have code below there way move view model avoiding code behind completely?
public void listview_click(object sender,routedeventargs e) { var item = (sender listview).selecteditem; if (item != null) { record record = item.datacontext record; if (record != null) { myviewmodel viewmodel = ((myviewmodel)this.datacontext); var result = viewmodel.performwork(record); } } }
note: record listviewitem
thanks, nick
bind listview.selecteditem
property property of type record
in viewmodel (such selectedrecord
) , perform work when selected record changes:
xaml:
<listview .... selecteditem="{binding selectedrecord}"/>
viewmodel:
private record _selectedrecord; public record selectedrecord { { return _selectedrecord; } set { _selectedrecord = value; onpropertychanged(); // call dowork() method here this.dowork(); } }
Comments
Post a Comment