c# - Set DataGridCell style depending on CheckBox Checked state in DataGridTemplateColumn -
i have datagrid data binding datatable columns set auto generate.
the first column has boolean data type replaced datagridtemplatecolumn checkbox datatemplate. works fine is.
however, want make datagridcell background red when checkbox not checked.
the problem is, have no idea how set checkbox's parent datagridcell style ischecked trigger.
wpf:
<window.resources> <datagridtemplatecolumn x:key="colselect"> <datagridtemplatecolumn.celltemplate> <datatemplate> <checkbox name="chkbxselect" horizontalalignment="center" verticalalignment="center" ischecked="{binding path=select, mode=twoway, updatesourcetrigger=propertychanged}" click="chkbxselect_click"> </checkbox> </datatemplate> </datagridtemplatecolumn.celltemplate> <datagridtemplatecolumn.headertemplate> <datatemplate> <checkbox x:name="chkbxselectall" content="select" horizontalalignment="center" verticalalignment="center" isthreestate="true" click="chkbxselectall_click" ischecked="{binding relativesource={relativesource ancestortype={x:type datagrid}}, path=datacontext.selectall}"> </checkbox> </datatemplate> </datagridtemplatecolumn.headertemplate> <datagridtemplatecolumn.cellstyle> <style targettype="{x:type datagridcell}"> <style.triggers> <trigger property="isselected" value="true"> <setter property="foreground" value="white"></setter> <setter property="background" value="darkgray"></setter> <setter property="borderbrush" value="red"></setter> <setter property="borderthickness" value="1"></setter> </trigger> </style.triggers> </style> </datagridtemplatecolumn.cellstyle> </datagridtemplatecolumn> </window.resources>
c# while datagrid column autogenerating:
datagridtemplatecolumn col = (datagridtemplatecolumn)resources["colselect"]; e.column = col; e.column.isreadonly = false;
update: far have figured out done using relativesource
, ancestortype
in binding. however, still trying make work.
well after struggling lot , not trying obvious solution. found it. relatively simple , easy.
just added datatrigger datagridcell style , done, wpf magic.
<datagridtemplatecolumn.cellstyle> <style targettype="{x:type datagridcell}"> <style.triggers> <trigger property="isselected" value="true"> <setter property="foreground" value="white"></setter> <setter property="background" value="darkgray"></setter> <setter property="borderbrush" value="red"></setter> <setter property="borderthickness" value="1"></setter> </trigger> <datatrigger binding="{binding path=select, mode=twoway, updatesourcetrigger=propertychanged}" value="false"> <setter property="background" value="red"/> </datatrigger> </style.triggers> </style> </datagridtemplatecolumn.cellstyle>
Comments
Post a Comment