c# - WPF commands globally available -
i have window
element contains ribbonmenue
. within window
there usercontrols
. in 1 of usercontrol
datagrid
. created icommand
lets me add , delete rows datagrid
.
the problem somehow need access these icommands
ribbonmenu
, can access them @ "higher level" (the window) since declared , bound viewmodel
bound usercontrol
.
how can create icommands
can called globally? note icommand
needs reference viewmodel
behind usercontrol
since need delete rows , on.
the traditional mvvm way "global commands" use compositecommand. you'll have globalcommands.cs file contains static class called globalcommands.
in it, you'll have icommand properties return compositecommand instance. then, vm interested in command can attach in thier constructor: globalcommands.somecommand.registercommand(...). ui attach globalcommands commands.
so globalcommands hold compositecommand empty shell / holder command , vms register normal relaycommand composite command , handle command. multiple vms can register same command , called.
the more advanced compositecommand implementations include iactiveaware feature can make compositecommand send canexecute/execute "active" vm's.
i believe compositecommand came prism, many folks (including myself) have broken out use in non prism applications.
Comments
Post a Comment