wpf - Command binding issue in CustomControl: CanExecute() fires, Execute() does not -


i'm binding button command, in controltemplate, execute() method in customcontrol. i'm using routedcommand, canexecute() fires, execute() never does. when customcontrol placed in main window, code works expected. when placed in usercontrol, have issue. have tried several ways wire buttons command (relaycommand etc) can't seem figure out what's wrong. appreciated.

for context, tokenizingtextbox control - fork of xceed open source version. button deleting token list of tokens.

the complete style of tokeniten (which contains button of interest):

    <style targettype="{x:type local:tokenitem}">       <setter property="background" value="#f3f7fd" />       <setter property="borderbrush" value="#bbd8fb" />       <setter property="borderthickness" value="1" />       <setter property="cursor" value="arrow" />       <setter property="padding" value="2,1,1,1" />       <setter property="margin" value="1,0" />       <setter property="template">          <setter.value>             <controltemplate targettype="{x:type local:tokenitem}">                <border background="{templatebinding background}"                        borderbrush="{templatebinding borderbrush}"                        borderthickness="{templatebinding borderthickness}"                        padding="{templatebinding padding}"                        cornerradius="0,0,5,5"                        margin="{templatebinding margin}"                        >                         <stackpanel orientation="horizontal" margin="1" x:name="myroot">                             <contentpresenter contenttemplate="{templatebinding contenttemplate}" />                              <button margin="3,0,0,0" cursor="hand"                                      command="{x:static local:tokenizedtextboxcommands.delete}" commandparameter="{templatebinding tokenkey}"                                      presentationtracesources.tracelevel="high">                         <!--<button.template>                            <controltemplate targettype="button">                               <contentpresenter />                            </controltemplate>                         </button.template>-->                         <image source="/resources/delete8.png" width="8" height="8" />                      </button>                   </stackpanel>                </border>             </controltemplate>          </setter.value>       </setter>    </style>     

the static command:

  public static class tokenizedtextboxcommands   {     private static routedcommand _deletecommand = new routedcommand();      public static routedcommand delete => _deletecommand;   } 

the custom control inherits itemscontrol. in non-static constructor, wire static delete command deletetoken method:

public tokenizedtextbox() {     commandbindings.add(new commandbinding(tokenizedtextboxcommands.delete, deletetoken, candelete)); } 

finally candelete sets canexecute true:

    private void candelete(object sender, canexecuteroutedeventargs canexecuteroutedeventargs)     {         canexecuteroutedeventargs.canexecute = true;     } 

and deletetoken - functionality omitted, signature important thing here:

    private void deletetoken(object sender, executedroutedeventargs e)     {         ...     } 

so, enough information interested in providing guidance/suggestions. thanks.

little interest here hired mentor through pluralsight. bindings correct, customcontrol had richtextbox capturing mouse click. fixed issue using behavior targeting button's previewmousedown.


Comments