c# - Cannot Access Command in WPF UserControl's DataGrid Row -
i not able execute command (testcommand) in user control may because datagrid of usercontrol using filedetailslist(list)
the following wpf form , using mvvm
<window> <tabcontrol> <tabitem header="result"> <usercontrol:filesearchresult></usercontrol:filesearchresult> </tabitem> </tabcontrol> </window>
the below user control
<usercontrol> <grid> <grid.rowdefinitions> <rowdefinition height="*"></rowdefinition> <rowdefinition height="30"></rowdefinition> </grid.rowdefinitions <datagrid grid.row="0" itemsource="{binding filedetailslist}"> <datagrid.columns> <datagridtemplatecolumn> <datagridtemplatecolumn.celltemplate> <datatemplate> <button>view <button.contextmenu> <contextmenu fontsize="11"> <menuitem command="{binding testcommand}" commandparameter="{binding fileid}" header="splitter errors"/> </contextmenu> </button.contextmenu> </button> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> <datagridtextcolumn binding="{binding fileid}" header="file id"/> <datagridtextcolumn binding="{binding filename}" header="file name"/> <datagrid.columns> <datagrid grid.row="0"> </grid> </usercontrol>
the below view model
public class filedetailsviewmodel : inotifypropertychanged { private list<filedetail> _filedetailslist = new list<filedetail>(); public relaycommand<int32> testcommand { get; private set; } public filedetailsviewmodel() { testcommand = new relaycommand<int>(opentestcommand); } private void opentestcommand(int fileid) { ///some code } public list<filedetail> filedetailslist { { return _filedetailslist; } set { _filedetailslist = value; notifypropertychanged("filedetailslist"); } } public event propertychangedeventhandler propertychanged; protected void notifypropertychanged(string property) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(property)); } } }
please solve issue
you try work 100%,
<button tag="{binding path=datacontext, relativesource={relativesource ancestortype={x:type datagrid}}}"> view <!-- binding relativesource={relativesource ancestortype={x:type datagrid}}, path=datacontext. --> <button.contextmenu> <contextmenu fontsize="11"> <menuitem command="{binding path=placementtarget.tag.testcommand, relativesource={relativesource ancestortype={x:type contextmenu}}}" commandparameter="{binding fileid}" header="splitter errors" /> </contextmenu> </button.contextmenu> </button>
Comments
Post a Comment