c# - Grouping related ICommands -


i'm new c# , first wpf project. following this tutorial (using implementation of relaycommand , attempting use mvvm. implementing clone of standard windows calculator. find way group functionality of similar buttons doing seems clumsy.

for exmaple, here my xaml of 3 buttons

<button content="_7" focusable ="false" command="{binding seven}" style="{dynamicresource numberbutton}" margin="0,134,184,129"/> <button content="_8" focusable ="false" command="{binding eight}" style="{dynamicresource numberbutton}" margin="46,134,138,129"/> <button content="_9" focusable ="false" command="{binding nine}" style="{dynamicresource numberbutton}" margin="92,134,92,129"/> 

here icommands those:

public icommand 9 { { return new relaycommand(nineexecute); } } public icommand 8 { { return new relaycommand(eightexecute); } } public icommand 7 { { return new relaycommand(sevenexecute); } } 

and methods:

void nineexecute() {    numberpressed("9"); } void eightexecute() {    numberpressed("8"); } void sevenexecute() {    numberpressed("7"); } 

what should investigate in order group similar function buttons such numbers single icommand, single method can determine sender - while still not putting code behind in window class article warns against.

xlam code button (supposing defined data context):

    <....datacontext>         <loc:commands/>     </....datacontext>     <button content="_9"              command="{binding path=showmeabox}"              commandparameter="{binding relativesource={relativesource mode=self}, path=content}"/> 

our dummy command (using relaycommand<t> provided link):

public class commands {     private static readonly icommand _showshowboxcommand =          new relaycommand<string>(str => messagebox.show(str));     public static icommand showmeabox { { return _showshowboxcommand; } } } 

that's it.

fyi.

  1. it's seems explicitly specify button size bad practice. position buttons use stack or wrap panel, or grid/uniformgrid.
  2. read info on styles , templates increase code reuse.

example:

 <uniformgrid columns="3" rows="3">     <uniformgrid.datacontext>         <loc:commands/>     </uniformgrid.datacontext>     <uniformgrid.resources>         <style targettype="button">             <setter property="command" value="{binding showmeabox}"/>             <setter property="commandparameter" value="{binding relativesource={relativesource mode=self}, path=content}"/>         </style>     </uniformgrid.resources>     <button content="_1"/>     <button content="_2"/>     <button content="_3"/>     <button content="_4"/>     <button content="_5"/>     <button content="_6"/>     <button content="_7"/>     <button content="_8"/>     <button content="_9"/> </uniformgrid> 
  1. may it's possible bind enumerable.range(0,10) populate control automatically in mvvm fashion.

good luck!


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -