Thursday, 15 March 2012

c# - prism InvokeCommandAction behaviour ignoring IsEnabled property (possible bug?) -



c# - prism InvokeCommandAction behaviour ignoring IsEnabled property (possible bug?) -

i trying bottom of why isenabled property of checkbox beingness ignored when using prism invokecommandaction behaviour.

essentially, if utilize standard invokecommandaction behaviour works fine, if switch prism invokecommandaction, isenabled property ignored.

to replicate this, took commanding quickstart prism , changed orderseditorview this:

<stackpanel orientation="vertical"> <listview automationproperties.automationid="orderlistview" itemssource="{binding orders}" selectionmode="single" width="auto" height="auto" > <i:interaction.triggers> <i:eventtrigger eventname="keyup"> <prism:invokecommandaction command="{binding processordercommand}" triggerparameterpath="key"/> </i:eventtrigger> </i:interaction.triggers> <listview.itemtemplate> <datatemplate datatype="{x:type models:orderviewmodel}"> <textblock text="{binding ordername}" /> </datatemplate> </listview.itemtemplate> </listview> <checkbox isenabled="false" content="prism" > <i:interaction.triggers> <i:eventtrigger eventname="checked"> <prism:invokecommandaction command="{binding processordercommand}" /> </i:eventtrigger> </i:interaction.triggers> </checkbox> <checkbox isenabled="false" content="interactivity"> <i:interaction.triggers> <i:eventtrigger eventname="checked"> <i:invokecommandaction command="{binding processordercommand}" /> </i:eventtrigger> </i:interaction.triggers> </checkbox> </stackpanel>

so i've wrapped original listview in stack panel , added 2 checkboxes bind existing processordercommand command invokecommandaction behaviour. checkbox using interactivity invokecommandaction correctly disabled (the isenabled property correctly set false), checkbox using prism invokecommandaction enabled, though isenabled property set false.

now basic example, in real life behaviour passing parameters command (which why want utilize prism version) , isenabled property bound property on viewmodel. i've reduced illustration basic elements. such specific unusual behaviour, certainly i'm doing wrong here? can't bug in prism can it?

i know question little old, today co-worker , ran same issue @ work, did inquire stackoverflow , couldn't find solution. after figure out, decided post solution here in case else encounters kind of problem in future.

as @georg piwonka says, behavior design in prism, can utilize property binding , after raising propertychanged event, phone call raisecanexecutechanged method of command in order reevaluate canexecute method used when created command.

so, allow have orderprocessingviewmodel class containing processordercommand command (delegatecommand)

public class orderprocessingviewmodel { public delegatecommand<keyeventargs> processordercommand; private string targetproperty; public orderprocessingviewmodel() { processordercommand = new delegatecommand<keyeventargs>(processorder, canprocessorder); } private order ordertoprocess { get; set; } public string targetproperty { { homecoming targetproperty; } set { if (!equals(targetproperty, value)) { targetproperty = value; onpropertychanged("targetproperty"); //important: allow delegate command phone call canexecute method can update enabled property. processordercommand.raisecanexecutechanged(); } } } private bool canprocessorder(keyeventargs arg) { //todo: here check when enable or disable check box var mustenable = ordertoprocess != null; //the value returned method result assigned isenable property command's related object homecoming mustenable; } private void processorder(keyeventargs obj) { //do stuff process order } }

the key concept here not go against prism. if want enable related command whenever target property changes, should re-run canexcecute method delegatecommand command.

prism has mechanism purpose, thing need phone call raisecanexecutechanged processordercommand command , if result of method true, related control, in case checkbox enabled.

c# prism

No comments:

Post a Comment