mvvm - WPF Command Parameter for button inside ItemsControl -
i developing wpf application using mvvm design pattern. in application have itemscontrol display several buttons (number of buttons not constant, according logic flow of application). itemscontrol binded linkedlist in viewmodel. able define command buttons , able command handler within viewmodel if command has no parameters... need able know button has been clicked , therefore have command parameter.
how need define view ? syntax commandparameter , parameter can used button content ? command signature in viewmodel ?
please note using relaycommand mvvm light. relvant code of view here. in advance
<usercontrol x:class="museum.controls.categoriesview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="130" d:designwidth="418"> <usercontrol.resources> <datatemplate x:key="categoriesbuttonstemplate"> <button content="{binding }" command="{binding relativesource={relativesource ancestortype={x:type itemscontrol}}, path=datacontext.categoryselectedcommand}" /> </datatemplate> </usercontrol.resources> <grid showgridlines="true"> <grid.rowdefinitions> <rowdefinition height="50"></rowdefinition> <rowdefinition height=" 40"></rowdefinition> <rowdefinition height=" 40"></rowdefinition> </grid.rowdefinitions> <grid x:name="subsubjectgrid" grid.row="0"></grid> <grid x:name="categoriesgrid" grid.row="1"> <itemscontrol grid.row="1" itemssource="{binding path=categoriesbuttons}" itemtemplate="{staticresource categoriesbuttonstemplate}" > <itemscontrol.itemspanel> <itemspaneltemplate> <stackpanel orientation="horizontal" horizontalalignment="stretch"/> </itemspaneltemplate> </itemscontrol.itemspanel> </itemscontrol> </grid> <grid x:name="selectedcategorygrid" grid.row="2"></grid> </grid> </usercontrol>
you can use commandparameter="{binding }" this
<datatemplate x:key="categoriesbuttonstemplate"> <button content="{binding }" command="{binding relativesource={relativesource ancestortype={x:type itemscontrol}}, path=datacontext.categoryselectedcommand}" commandparameter="{binding }" /> </datatemplate>
Comments
Post a Comment