wpf - How to change IsReadOnly by Trigger -
can enlight me way following doesn't work
i'm using mvvm pattern
after executing code i'm still able check , uncheck checkboxes in datagridcheckboxcolumn
property (part of viewmodel)
public bool nocheckpermission { { return false; } // example }
xaml (my view no code behind)
<datagrid height="300" autogeneratecolumns="false" selectionmode="single" itemssource="{binding itemlist, updatesourcetrigger=propertychanged}"> <datagrid.resources> <style targettype="{x:type datagridcheckboxcolumn}"> <style.triggers> <datatrigger binding="{binding nocheckpermission}" value="false"> <setter property="isreadonly" value="true"/> </datatrigger> <datatrigger binding="{binding nocheckpermission}" value="true"> <setter property="isreadonly" value="false"/> </datatrigger> </style.triggers> </style> </datagrid.resources> <datagrid.columns> <datagridcheckboxcolumn width="60" header="freigabe" binding="{binding freigegebenl}" canuserreorder="false"> </datagridcheckboxcolumn> </datagrid.columns> </datagrid>
edit: after doing blindmeis , suggested. got current xaml
<datagrid height="300" margin="12" autogeneratecolumns="false" canuseraddrows="false" canuserdeleterows="false" canuserresizecolumns="false" canuserresizerows="false" canuserreordercolumns="false" x:name="grd" itemssource="{binding itemlist, updatesourcetrigger=propertychanged}" selecteditem="{binding selecteditem, updatesourcetrigger=propertychanged}" selectionmode="single" selectionchanged="datagrid_selectionchanged" tabindex="2"> <datagrid.columns> <datagridcheckboxcolumn width="60" isreadonly="{binding datacontext, path=nocheckpermission, elementname=grd}" binding="{binding freigegebenl}" header="freigabe" canuserreorder="false"> </datagridcheckboxcolumn> </datagrid.columns> </datagrid>
and error in output window:
system.windows.data error: 2 : cannot find governing frameworkelement or frameworkcontentelement target element. bindingexpression:path=nocheckpermission; dataitem=null; target element 'datagridcheckboxcolumn' (hashcode=35155182); target property 'isreadonly' (type 'boolean')
first
noticed property in wrong vm (it in child vm rows) fixing doesn't solved problem
finally
after reading this article able solve
in condensed form
class
public class bindingproxy : freezable { #region overrides of freezable protected override freezable createinstancecore() { return new bindingproxy(); } #endregion public object data { { return (object)getvalue(dataproperty); } set { setvalue(dataproperty, value); } } // using dependencyproperty backing store data. enables animation, styling, binding, etc... public static readonly dependencyproperty dataproperty = dependencyproperty.register("data", typeof(object), typeof(bindingproxy), new uipropertymetadata(null)); }
xaml
resources
<datagrid.resources> <local:bindingproxy x:key="proxy" data="{binding}" /> </datagrid.resources>
column
<datagridcheckboxcolumn width="60" header="freigabe" canuserreorder="false" isreadonly="{binding data.nocheckpermission, source={staticresource proxy}}" binding="{binding freigegebenl}"/>
Comments
Post a Comment