wpf - DataGrid bind command to cell click -
here's quick question. have datagrid looks this:
<datagrid itemssource="{binding path=sections}" autogeneratecolumns="false"> <datagrid.columns> <datagridtextcolumn binding="{binding path=importname, mode=oneway}" header="imported" /> <datagridtextcolumn binding="{binding path=foundname, mode=twoway}" header="suggested" /> </datagrid.columns> </datagrid>
i want bind "suggested" column cells command in vm, each time user clicks cell editing, command execute , show dialog user. i've found interesting solution similar problem described here: datagrid bind command row select
i fact manages xaml without code-behind attaches cell editing event. unfortunately, i've no idea how convert in way allow me bind command cells in specific column, rather entire row. advice in regards that?
you can use beginningedit
event in datagrid
control handle scenario. event fires before row or cell enters edit mode. can identify selected column eventargs. example:
private void dgname_beginningedit(object sender, datagridbeginningediteventargs e) { if (e.column.header.tostring() == "suggested") { //do operation } }
if using mvvm pattern, there options pass eventargs vm. if uusing mvvmlight toolkit, there option called passeventargs
, set true
.
in vm,
//relay command
private relaycommand<datagridbeginningediteventargs> _cellbeginningeditcommand; public relaycommand<datagridbeginningediteventargs> cellbeginningeditcommand { { return _cellbeginningeditcommand ?? (_cellbeginningeditcommand = new relaycommand<datagridbeginningediteventargs>(cellbeginningeditmethod)); } }
//command handler
private void cellbeginningeditmethod(datagridbeginningediteventargs args) { if(args.column.header.tostring() == "suggested") { //do operation } }
Comments
Post a Comment