WPF. Changing CheckBox IsChecked through MultiBinding doesn't triger CheckBox Command -


i have datagrid in wpf app view use check boxes in row headers.

<datagrid.rowheadertemplate> <datatemplate>     <grid >         <checkbox borderthickness="0"                     command="{binding datacontext.assignpartstogroupcommand, relativesource={relativesource findancestor,                                                                                ancestortype={x:type usercontrol}}}"                                                                               >             <checkbox.commandparameter>                 <multibinding converter="{staticresource partsgroupassignconverter}">                     <binding path="ischecked" relativesource="{relativesource self}" mode="oneway"/>                     <binding relativesource="{relativesource mode=findancestor,                                                               ancestortype={x:type datagridrow}}"                               path="datacontext" mode="oneway"/>                 </multibinding>             </checkbox.commandparameter>             <checkbox.ischecked>                 <multibinding  converter="{staticresource partsgroupassignedconverter}"  mode="oneway">                     <binding elementname="partsgroupgrid" path="selecteditem.id"></binding>                     <binding relativesource="{relativesource mode=findancestor,                                                               ancestortype={x:type datagridrow}}"                               path="datacontext" mode="oneway"/>                     <binding path="isselected" mode="oneway"                              relativesource="{relativesource findancestor,                               ancestortype={x:type datagridrow}}"                              />                 </multibinding>             </checkbox.ischecked>         </checkbox>     </grid> </datatemplate> 

as can see bind checkbox property "isselected" multiple values , 1 of them datagrid row selection:

   <binding path="isselected"              mode="oneway"             relativesource="{relativesource findancestor,                                   ancestortype={x:type datagridrow}}"                                  /> 

my problem - command linked checkbox not triggered when check checkbox selecting row. triggers when manually (with mouse). how can solve this?

according checkbox source codes, desired approach not supported - command called after clicking on it.

however, can create small checkbox descendant achieve behavior need. descendant track changes of checkbox.ischecked property , execute command.

you can this:

    public class mycheckbox : checkbox {     static mycheckbox() {         ischeckedproperty.overridemetadata(typeof(mycheckbox), new frameworkpropertymetadata((o, e) => ((mycheckbox)o).onischeckedchanged()));     }      readonly locker togglelocker = new locker();     readonly locker clicklocker = new locker();      void onischeckedchanged() {         if (clicklocker.islocked)             return;         using (togglelocker.lock()) {             onclick();         }     }      protected override void ontoggle() {         if (togglelocker.islocked)             return;         base.ontoggle();     }      protected override void onclick() {         using (clicklocker.lock()) {             base.onclick();         }     } } 

and locker class:

    class locker : idisposable {     int count = 0;     public bool islocked { { return count != 0; } }     public idisposable lock() {         count++;         return this;     }     public void dispose() { count--; } } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -