wpf - How to access command from MainWindow level in another Window? -


i trying access commands defined in mainwindow.xaml in window. able grayed out titles of these commands. wondering should should done in order full access.

sample of command:

public partial class mainwindow : window {     public static routeduicommand addcommand1 = new routeduicommand("command ", "command1", typeof(mainwindow));      public mainwindow()     {         initializecomponent();         this.commandbindings.add(new commandbinding(addcommand1, addcommand1executed));     }      private void addcommand1executed(object sender, executedroutedeventargs e)     {         addnewitem picker = new addnewitem();         picker.showdialog();     } 

i access these command in style through databinding:

<menu x:name="taskmenucontainer"><menuitem x:name="menuitem" header="tasks" itemssource="{binding}" template="{dynamicresource taskmenutoplevelheadertemplatekey}"> <menuitem.itemcontainerstyle>     <style targettype="{x:type menuitem}">         <setter property="command" value="{binding}" />         <setter property="header" value="{binding text, relativesource={relativesource self}}" />         <setter property="commandtarget" value="{binding relativesource={relativesource self}}"/>     </style> </menuitem.itemcontainerstyle> 

these commands work in pages loaded inside mainwindow.xaml through frame. however, if have pop window not part of mainwindow.xaml these commands grayed out , not functional anymore (cannot executed). advice highly appreciated!

the way define command, define particular window. if want handle command globally, @ application level, can use commandmanager.registerclasscommandbinding:

first, define command in separate static class:

public static class globalcommands {     public static routeduicommand addcommand1 = new routeduicommand("command ", "command1", typeof(mainwindow)); } 

then, in window or whatever place want put command logic, register command handlers:

public partial class mainwindow : window {     static mainwindow()     {        commandmanager.registerclasscommandbinding(typeof(window), new commandbinding(globalcommands.addcommand1, addcommand1executed, canaddexecute));     }      private static void addcommand1executed(object sender, executedroutedeventargs e)     {         addnewitem picker = new addnewitem();         picker.showdialog();     } } 

and in menu style should change binding x:static:

<setter property="command" value="{x:static my:globalcommands.addcommand1}" /> 


when command routed, when checking command bindings in each active element in ui, bindings registered each element's class checked. registering binding here, can cause every instance of class able handle specific command.

so, in above example, type window used , cause routing find command binding in any instance of window, once routing reaches instance in search command binding.

you instead, example, restrict handling specific subclass of window, command bound in instance of subclass. or can use other ui element type, that presence of specific type of element cause event handled. set owning type registered command binding appropriately specific needs.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -