CheckBox Command Behaviors for Silverlight MVVM Pattern -


i trying detect when item checked, , which item checked in listbox using silverlight 4 , prism framework. found example on creating behaviors, , tried follow nothing happening in debugger. have 3 questions:

  1. why isn't command executing?
  2. how determine item checked (i.e. pass command parameter)?
  3. how debug this? (i.e. can put break points begin stepping this)

here code:

view:

        <listbox x:name="mylistbox" itemssource="{binding panelitems, mode=twoway}">             <listbox.itemtemplate>                 <datatemplate>                     <stackpanel orientation="horizontal">                         <checkbox ischecked="{binding enabled}" my:checked.command="{binding check}"  />                         <textblock x:name="displayname" text="{binding displayname}"/>                     </stackpanel>                 </datatemplate>             </listbox.itemtemplate>         </listbox> 

viewmodel:

public mainpageviewmodel() {     _panelitems.add( new panelitem     {         enabled = true,         displayname = "test1"     } );      check = new delegatecommand<object>( itemchecked ); }  public void itemchecked( object o ) { //do stuff }  public delegatecommand<object> check { get; set; } 

behavior class

public class checkedbehavior : commandbehaviorbase<checkbox>     {         public checkedbehavior( checkbox element )             : base( element )         {             element.checked +=new routedeventhandler(element_checked);         }          void element_checked( object sender, routedeventargs e )         {             base.executecommand();         }                    } 

command class

public static class checked {     public static icommand getcommand( dependencyobject obj )     {         return (icommand) obj.getvalue( commandproperty );     }      public static void setcommand( dependencyobject obj, icommand value )     {         obj.setvalue( commandproperty, value );     }      public static readonly dependencyproperty commandproperty =             dependencyproperty.registerattached( "command", typeof( checkbox ), typeof( checked ), new             propertymetadata( onsetcommandcallback ) );      public static readonly dependencyproperty checkedcommandbehaviorproperty =                 dependencyproperty.registerattached( "checkedcommandbehavior", typeof( checkedbehavior ), typeof( checked ), null );      private static void onsetcommandcallback( dependencyobject dependencyobject, dependencypropertychangedeventargs e )     {         checkbox element = dependencyobject checkbox;         if( element != null )         {             checkedbehavior behavior = getorcreatebehavior( element );             behavior.command = e.newvalue icommand;         }     }     private static checkedbehavior getorcreatebehavior( checkbox element )     {         checkedbehavior behavior = element.getvalue( checkedcommandbehaviorproperty ) checkedbehavior;         if( behavior == null )         {             behavior = new checkedbehavior( element );             element.setvalue( checkedcommandbehaviorproperty, behavior );         }          return behavior;     }     public static checkedbehavior getcheckcommandbehavior( dependencyobject obj )     {         return (checkedbehavior) obj.getvalue( checkedcommandbehaviorproperty );     }     public static void setcheckcommandbehavior( dependencyobject obj, checkedbehavior value )     {            obj.setvalue( checkedcommandbehaviorproperty, value );     }                

}

your sample not enough repro on pc, here things i'd correct first:

  • the bindings in datatemplate missing ", mode=twoway" if want enabled property set in panelitem (- itemssource binding not need mode=twoway, minor detail)
  • the datacontext of itemtemplate panelitem instance, binding of check command seems wrong: there no check property on panelitem. binding should be: my:checked.command="{binding elementname=mylistbox, path=datacontext.check}

this kind of stuff hard debug. @ output window of vs; binding errors (path not found) displayed there. when have dp change callback (like onsetcommandcallback), breakpoint there tell how binding went.

edit: added after 1st comment (as can't use comment feature on pc have use now) command attached property defined type checkbox in checked class, check property in vm delegatecommand. agree wpf on type mismatch :-) property declaration this:

public static readonly dependencyproperty commandproperty =      dependencyproperty.registerattached(          "command", typeof( checkbox ),          typeof( checked ), new propertymetadata( onsetcommandcallback ) );  

the second parameter should property type, guess icommand in case.

out of curiosity: in onsetcommandcallback, don't care value set command property (which in e.newvalue). how relate instance of checkedbehavior check property of vm ?

edit after second comment: no, 2nd paragraph above not related question. maybe not make sense. can't figure out role of checkedbehavior.

concerning question of item checked/unchecked: need more precisely ? have panelitem instance, enabled property being set true or false through biding; checked items ones enabled=true.

edit after 3rd comment: explanation of needs. you're not using path parameter of binding attached property, write:

my:checked.command="{binding}" 

this way, e.newvalue bound panelitem in onsetcommandcallback. given checkedbehavior instance (in constructor), forward when calling execute of icommand.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -