wpf - bindable property for checkbox ischecked do not reflect when trigger used -
my bindable property revalsurfacechecked
not updated when set ischecked
property in trigger
<checkbox grid.row="1" grid.column="2" grid.columnspan="2" x:name="chkrevalsurface" content="export reval surface (if applicable)" horizontalalignment="left" verticalalignment="center"> <checkbox.isenabled> <multibinding converter="{staticresource revalsurfacecheckboxenableconverter}"> <binding elementname="chkexporttocsv" path="ischecked"></binding> <binding elementname="chkexporttoexcel" path="ischecked"></binding> </multibinding> </checkbox.isenabled> <checkbox.style> <style targettype="{x:type checkbox}"> <setter property="ischecked" value="{binding revalsurfacechecked}" /> <style.triggers> <trigger property="isenabled" value="false"> <setter property="ischecked" value="false"></setter> </trigger> </style.triggers> </style> </checkbox.style> </checkbox>
the logic here if check of 2 checkbox, chkrevalsurface
should enabled else stay disabled-written in converter. when chkrevalsurface
disabled should set unchecked (written in trigger) , revalsurfacechecked
property should set false
. if checked set true
in viewmodel.
butrevalsurfacechecked
is not set true or false.
the problem <setter property="ischecked" value="false"></setter>
clears original binding, checkbox
's ischecked
property ends being false
without changing underlying property.
you mentioned have viewmodel, assume chkexporttocsv
, chkexporttoexcel
checkbox
es bound property in viewmodel.
lets assume viewmodel:
public class mainwindowviewmodel : inotifypropertychanged { private bool _revalsurfacechecked; public bool revalsurfacechecked { { return _revalsurfacechecked; } set { _revalsurfacechecked = value; onpropertychanged(); } } private bool _ischkexporttocsvchecked; public bool ischkexporttocsvchecked { { return _ischkexporttocsvchecked; } set { _ischkexporttocsvchecked = value; revalsurfacechecked = revalsurfacechecked && (_ischkexporttocsvchecked || _ischkexporttoexcelchecked); onpropertychanged(); } } private bool _ischkexporttoexcelchecked; public bool ischkexporttoexcelchecked { { return _ischkexporttoexcelchecked; } set { _ischkexporttoexcelchecked = value; revalsurfacechecked = revalsurfacechecked && (_ischkexporttocsvchecked || _ischkexporttoexcelchecked); onpropertychanged(); } } public event propertychangedeventhandler propertychanged; private void onpropertychanged([callermembername] string propertyname = null) { if (propertychanged != null) { propertychanged.invoke(this, new propertychangedeventargs(propertyname)); } } }
and xaml:
<checkbox x:name="chkexporttocsv" content="chkexporttocsv" ischecked="{binding ischkexporttocsvchecked}" horizontalalignment="left" verticalalignment="center" /> <checkbox x:name="chkexporttoexcel" content="chkexporttoexcel" ischecked="{binding ischkexporttoexcelchecked}" horizontalalignment="left" verticalalignment="center" /> <checkbox x:name="chkrevalsurface" content="export reval surface (if applicable)" horizontalalignment="left" verticalalignment="center" ischecked="{binding revalsurfacechecked}"> <checkbox.isenabled> <multibinding converter="{staticresource anytrueconverter}"> <binding elementname="chkexporttocsv" path="ischecked"></binding> <binding elementname="chkexporttoexcel" path="ischecked"></binding> </multibinding> </checkbox.isenabled> </checkbox>
i'm binding chkexporttocsv
's ischecked
property ischkexporttocsvchecked
property in viewmodel , chkexporttoexcel
's ischecked
ischkexporttoexcelchecked
property.
i removed style
chkrevalsurface
, isenabled
use converter gives true
if of inputs true
. assume revalsurfacecheckboxenableconverter
same, i'll include here:
public class anytrueconverter : imultivalueconverter { public object convert(object[] values, type targettype, object parameter, cultureinfo culture) { return values != null && values.oftype<bool>().any(b => b); } public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture) { throw new notimplementedexception(); } }
the trick in setter of ischkexporttocsvchecked
, ischkexporttoexcelchecked
:
revalsurfacechecked = revalsurfacechecked && (_ischkexporttocsvchecked || _ischkexporttoexcelchecked);
we moved logic datatrigger
here. should solve problem. tested , works me.
Comments
Post a Comment