wpf - Conditional cell editing in xamDataGrid -
i'm using xamdatagrid. want disable cell status column, if value dbnull. issue seems fieldsettings, i'm not able pass correct value cell converter. here code :
xaml :
<window.resources> <dbnullconverter:dbnulltobooleanconverter x:key="nulltobooleanconverter" /> </window.resources> <grid> <dockpanel> <igdp:xamdatagrid x:name="griddata" datasource="{binding path=tempdatatable}"> <igdp:xamdatagrid.fieldlayoutsettings> <igdp:fieldlayoutsettings autogeneratefields="true"/> </igdp:xamdatagrid.fieldlayoutsettings> <igdp:xamdatagrid.fieldsettings> <igdp:fieldsettings allowedit="true" /> </igdp:xamdatagrid.fieldsettings> <igdp:xamdatagrid.fieldlayouts> <igdp:fieldlayout> <igdp:field name="status" label="status"> <igdp:field.settings> <igdp:fieldsettings allowedit="{binding source={relativesource self}, path=self, converter={staticresource nulltobooleanconverter}}" /> </igdp:field.settings> </igdp:field> <igdp:field name="rowid" /> <igdp:field name="result" label="value" /> <igdp:field name="hasrowbeenedited" label="edited ?"> <igdp:field.settings> <igdp:fieldsettings editortype="{x:type igeditors:xamcheckeditor}"/> </igdp:field.settings> </igdp:field> </igdp:fieldlayout> </igdp:xamdatagrid.fieldlayouts> </igdp:xamdatagrid> </dockpanel> </grid>
edit :
the error in line :
<igdp:fieldsettings allowedit="{binding source={relativesource self}, path=self, converter={staticresource nulltobooleanconverter}}" />
viewmodel :
public class dbnullconverterviewmodel : inotifypropertychanged { private datatable tempdatatable; public datatable tempdatatable { { return tempdatatable; } set { tempdatatable = value; raisedpropertychanged("tempdatatable"); } } public dbnullconverterviewmodel() { tempdatatable = new datatable(); getvalue(); } private void getvalue() { tempdatatable.columns.add(new datacolumn("rowid", typeof(int32))); tempdatatable.columns.add(new datacolumn("status", typeof(string))); tempdatatable.columns.add(new datacolumn("statusnew", typeof(string))); tempdatatable.columns.add(new datacolumn("hasrowbeenedited", typeof(bool))); datarow row = tempdatatable.newrow(); row["rowid"] = 1; row["status"] = "active"; row["statusnew"] = "new"; row["hasrowbeenedited"] = true; tempdatatable.rows.add(row); tempdatatable.acceptchanges(); datarow row1 = tempdatatable.newrow(); row1["rowid"] = 2; row1["status"] = dbnull.value; row1["statusnew"] = null; row1["hasrowbeenedited"] = dbnull.value; tempdatatable.rows.add(row1); tempdatatable.acceptchanges(); raisedpropertychanged("tempdatatable"); } }
converter :
public class dbnulltobooleanconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { if (value == dbnull.value) return false; return true; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } }
imp : i'm looking pure viewmodel solution.
first of all, binding allowedit
won't work because applies cells of column. need more local approach.
also, binding incorrect. should read that:
{binding path=dataitem[status], converter={staticresource nulltobooleanconverter}}
there post @ infragistics tries achieve similar. in gist, need setting custom control template cellvaluepresenter
, add trigger:
<controltemplate.triggers> <datatrigger binding="{binding dataitem[status], converter={staticresource nulltobooleanconverter}}" value="true"> <setter property="igeditors:valueeditor.isreadonly" value="false" /> </datatrigger> </controltemplate.triggers>
additionally, derive xamdatagrid , add piece of functionality ensure isreadonly works intended:
protected override void oneditmodestarting(infragistics.windows.datapresenter.events.editmodestartingeventargs args) { var cell = args.cell; var celleditor = infragistics.windows.datapresenter.cellvaluepresenter.fromcell(cell).editor; if (celleditor != null && !celleditor.isreadonly) base.oneditmodestarting(args); else args.cancel=true; }
when setting custom control template cellvaluepresenter
, need need include default implementation can find in %program files%\infragistics\netadvantage {version}\wpf\defaultstyles\datapresenter\datapresentergeneric_express.xaml
@ <style targettype="{x:type igdp:cellvaluepresenter}">
.
Comments
Post a Comment