c# - MenuItem in Window, CommandBinding in UserControl -


i have window:

<window x:class="somenamespace.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     height="350" width="525"> <window.commandbindings>     <commandbinding command="applicationcommands.copy"         canexecute="commandcanexecute" executed="commandexecuted"/> </window.commandbindings> <dockpanel>     <menu dockpanel.dock="top">         <menuitem header="file">             <menuitem command="applicationcommands.copy"/>         </menuitem>     </menu> </dockpanel> </window> 

with code behind:

void commandcanexecute(object sender, canexecuteroutedeventargs e) {     e.canexecute = true; }  void commandexecuted(object sender, eventargs e) {     messagebox.show("done!"); } 

and works way expect. can use menuitem or ctrl+c input binding run command.

but class has gotten big, , decide refactor. moved code behind user control. here's new window:

<window x:class="somenamespace.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:my="clr-namespace:somenamespace"     height="350" width="525"> <dockpanel>     <menu dockpanel.dock="top">         <menuitem header="file">             <menuitem command="applicationcommands.copy"/>         </menuitem>     </menu>     <my:usercontrol1/> </dockpanel> </window> 

and usercontrol:

<usercontrol x:class="imagedecompilesandbox.usercontrol1"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <usercontrol.commandbindings>         <commandbinding command="applicationcommands.copy"             canexecute="commandcanexecute" executed="commandexecuted"/>     </usercontrol.commandbindings> </usercontrol> 

basically, same, except commandbinding moved window usercontrol , 2 command methods pushed down user control.

question: why above not work? why user control's command not picked window? how menuitem / keybindings window work command execution in user control?

thanks dkozl, able find way make work.

the trick indeed adding commandbinding window. instead of declaring them in window, can't window doesn't know methods being used executed , canexecute, added bindings control window:

commandbindings.addrange(_usercontrol1.commandbindings); 

i find one-line hack need, lets me keep command controls , keybindings in window while moving command implementation control.

thanks dkozl!


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -