Bizarre Command IssueI have a window that is Outlook like, it is broken into 2 regions (a side bar, and a main content region).  The regions are populated with various user controls, depending on the current task being worked on.  The main menu is also a user control, which allows new menu items to be added to it, or have all its &quot;extra&quot; items cleared. I have defined commands to load each of these tasks (i.e. ViewEstimates or ViewJobs commands).  The command bindings for all these tasks are defined in the window itself, so when the command is executed by an element in one of the user controls, it bubbles up to the window before being intercepted.<br><br>So, if the user clicks an element that has the ViewEstimates command assigned to it, the sidebar is loaded with a treeview of all estimates, the main content is loaded with the currently selected estimate details, and some menu items are added to the main menu (to add a new estimate, and view estimates).  <br><br>Everything works as expected, accept one issue I really can't even begin to figure out what's going on....<br><br>If &quot;ViewEstimates&quot; is executed from the &quot;Home&quot; user control, the commands that assigned to the new menu items are always disabled (and only the commands in the menu items, anywhere else they are linked, works correctly)...period.  If &quot;ViewEstimates&quot; is executed from anywhere else (like a button in the sidebar), which calls the exact same code as if it's called from the &quot;Home&quot; control, the menu items work correctly.<br><br>The reason I am stumped, is if it's executeding the EXACT same code, how does the result differ like this?  I know the &quot;ViewEstimates&quot; command is being executed and handled, because if it weren't the &quot;ViewEstimates&quot; GUI wouldn't load.  There is only one set of code that runs when the &quot;ViewCommands&quot; command is executed...regardless of where it's executeds from, so what could be different?<br><br>Any ideas?<br>© 2009 Microsoft Corporation. All rights reserved.Tue, 08 Sep 2009 15:59:51 Zf898ab7f-1282-48bb-a248-7ce1caa05cf6http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#f898ab7f-1282-48bb-a248-7ce1caa05cf6http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#f898ab7f-1282-48bb-a248-7ce1caa05cf6uncle hammyhttp://social.msdn.microsoft.com/Profile/en-US/?user=uncle%20hammyBizarre Command IssueI have a window that is Outlook like, it is broken into 2 regions (a side bar, and a main content region).  The regions are populated with various user controls, depending on the current task being worked on.  The main menu is also a user control, which allows new menu items to be added to it, or have all its &quot;extra&quot; items cleared. I have defined commands to load each of these tasks (i.e. ViewEstimates or ViewJobs commands).  The command bindings for all these tasks are defined in the window itself, so when the command is executed by an element in one of the user controls, it bubbles up to the window before being intercepted.<br><br>So, if the user clicks an element that has the ViewEstimates command assigned to it, the sidebar is loaded with a treeview of all estimates, the main content is loaded with the currently selected estimate details, and some menu items are added to the main menu (to add a new estimate, and view estimates).  <br><br>Everything works as expected, accept one issue I really can't even begin to figure out what's going on....<br><br>If &quot;ViewEstimates&quot; is executed from the &quot;Home&quot; user control, the commands that assigned to the new menu items are always disabled (and only the commands in the menu items, anywhere else they are linked, works correctly)...period.  If &quot;ViewEstimates&quot; is executed from anywhere else (like a button in the sidebar), which calls the exact same code as if it's called from the &quot;Home&quot; control, the menu items work correctly.<br><br>The reason I am stumped, is if it's executeding the EXACT same code, how does the result differ like this?  I know the &quot;ViewEstimates&quot; command is being executed and handled, because if it weren't the &quot;ViewEstimates&quot; GUI wouldn't load.  There is only one set of code that runs when the &quot;ViewCommands&quot; command is executed...regardless of where it's executeds from, so what could be different?<br><br>Any ideas?<br>Tue, 15 Jan 2008 20:07:50 Z2008-01-15T20:07:50Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#07051e24-66ab-4f82-8047-67c17fdc76c4http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#07051e24-66ab-4f82-8047-67c17fdc76c4uncle hammyhttp://social.msdn.microsoft.com/Profile/en-US/?user=uncle%20hammyBizarre Command IssueOK, Ia lot of troubleshooting, and I have discovered the problem, though I am not sure how to fix it....<br><br>My first clue was that the trouble arises with controls that launch a &quot;change gui command&quot;, and the control is in my sidebar and windows &quot;content&quot; areas.  Part of my code that executes when the &quot;change gui&quot; commands are executed is to clear these content areas completely (including the control that launched the command), then reload them with the new content.  <br><br>If any of the controls that are cleared are command controls, that's when the CanExecute gets messed up.  I can only assume that clearing these controls, somehow breaks the command bindings, and thus any other controls subscribed to the commands in the window don't work as expected.<br><br>However, if I launch the command from a control not in either of these areas that get cleared (even if this happens after I &quot;break&quot; it), everything works correctly.  So I know the command binding are still in place, it seems that in senario a, the links get broken somehow.<br><br>Any ideas, how to safely remove these controls, keeping my command bindings to other controls in the window intact?<br>Wed, 16 Jan 2008 13:44:03 Z2008-01-16T13:44:03Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#2873f704-a854-490b-a40a-bed1a14a7266http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#2873f704-a854-490b-a40a-bed1a14a7266uncle hammyhttp://social.msdn.microsoft.com/Profile/en-US/?user=uncle%20hammyBizarre Command IssueTo better illiustrate, rather than trying to sort through my ramblings, here's a simple page that reproduces the problem.  <br><br>If you click &quot;Save&quot; from the toolbar, the content grid will be cleared, and all toolbar buttons remain enabled.<br><br>If you click save from the content grid, the gird will be cleared, and all toolbar buttons mysteriously are disabled. CommandManager.InvalidateRequerySuggested() does not resolve the issue.<br><br>However, as the Debug.Write shows, the CommandBinding.Count remains at 3<br><br> <div style="text-align:left"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Block</span></div> <p><span style="color:rgb(0,0,255)">&lt;</span><span style="color:rgb(255,0,0)">Window x:Class</span><span style="color:rgb(0,0,255)">=&quot;Testing_Window&quot;</span><br>    <span style="color:rgb(255,0,0)">xmlns</span><span style="color:rgb(0,0,255)">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span><br>    <span style="color:rgb(255,0,0)">xmlns:x</span><span style="color:rgb(0,0,255)">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span><br>    <span style="color:rgb(255,0,0)">Title</span><span style="color:rgb(0,0,255)">=&quot;Testing_Window&quot;</span> <br>    <span style="color:rgb(255,0,0)">Height</span><span style="color:rgb(0,0,255)">=&quot;300&quot;</span><br>    <span style="color:rgb(255,0,0)">Width</span><span style="color:rgb(0,0,255)">=&quot;300&quot;</span><br>    <span style="color:rgb(0,0,255)">&gt;</span><br><br>    &lt;<span style="color:rgb(128,0,0)">Window.CommandBindings</span>&gt;<br>        &lt;<span style="color:rgb(128,0,0)">CommandBinding</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Save&quot;</span> <span style="color:rgb(255,0,0)">CanExecute</span><span style="color:rgb(0,0,255)">=&quot;Command_CanExecute&quot;</span> <span style="color:rgb(255,0,0)">Executed</span><span style="color:rgb(0,0,255)">=&quot;Save_Executed&quot;</span>/&gt;<br>        &lt;<span style="color:rgb(128,0,0)">CommandBinding</span> <span style="color:rgb(255,0,0)">Command=</span><span style="color:rgb(0,0,255)">&quot;ApplicationCommands.Close&quot;</span> <span style="color:rgb(255,0,0)">CanExecute</span><span style="color:rgb(0,0,255)">=&quot;Command_CanExecute&quot;</span> <span style="color:rgb(255,0,0)">Executed</span><span style="color:rgb(0,0,255)">=&quot;Close_Executed&quot;</span>/&gt;<br>        &lt;C<span style="color:rgb(128,0,0)">ommandBinding</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Print&quot;</span> <span style="color:rgb(255,0,0)">CanExecute</span><span style="color:rgb(0,0,255)">=&quot;Command_CanExecute&quot;</span> <span style="color:rgb(255,0,0)">Executed</span><span style="color:rgb(0,0,255)">=&quot;Print_Executed&quot;</span>/&gt;<br>    &lt;/<span style="color:rgb(128,0,0)">Window.CommandBindings</span>&gt;<br><br>    &lt;<span style="color:rgb(128,0,0)">DockPanel</span> <span style="color:rgb(255,0,0)">LastChildFill</span><span style="color:rgb(0,0,255)">=&quot;True&quot;</span>&gt;<br>        &lt;<span style="color:rgb(128,0,0)">ToolBarTray</span> <span style="color:rgb(255,0,0)">DockPanel.Dock</span><span style="color:rgb(0,0,255)">=&quot;Top&quot;</span>&gt;<br>            &lt;<span style="color:rgb(128,0,0)">ToolBar</span>&gt;<br>                &lt;<span style="color:rgb(128,0,0)">Button</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Save&quot;</span> <span style="color:rgb(255,0,0)">Content</span><span style="color:rgb(0,0,255)">=&quot;Save&quot;</span>/&gt;<br>                &lt;<span style="color:rgb(128,0,0)">Button</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Close&quot;</span> <span style="color:rgb(255,0,0)">Content</span><span style="color:rgb(0,0,255)">=&quot;Close&quot;</span>/&gt;<br>                &lt;<span style="color:rgb(128,0,0)">Button</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Print&quot;</span> <span style="color:rgb(255,0,0)">Content</span><span style="color:rgb(0,0,255)">=&quot;Print&quot;/</span>&gt;<br>            &lt;/<span style="color:rgb(128,0,0)">ToolBar</span>&gt;<br>        &lt;/<span style="color:rgb(128,0,0)">ToolBarTray</span>&gt;<br>        &lt;<span style="color:rgb(128,0,0)">Grid</span> <span style="color:rgb(255,0,0)">x:Name</span><span style="color:rgb(0,0,255)">=&quot;TestGrid&quot;</span>&gt;<br>            &lt;<span style="color:rgb(128,0,0)">Button</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Save&quot;</span> <span style="color:rgb(255,0,0)">Content</span><span style="color:rgb(0,0,255)">=&quot;Save&quot;</span>/&gt;<br>        &lt;/<span style="color:rgb(128,0,0)">Grid</span>&gt;<br>    &lt;/<span style="color:rgb(128,0,0)">DockPanel</span>&gt;<br>&lt;/<span style="color:rgb(128,0,0)">Window</span>&gt;</p></div></div><br></div> <div style="text-align:left"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Block</span></div> <p><span style="color:rgb(0,0,255)">public partial class</span> <span style="color:rgb(51,153,102)">Testing_Window</span> : <span style="color:rgb(51,153,102)">Window</span><br>    {<br>        <span style="color:rgb(0,0,255)">public</span> Testing_Window()<br>        {<br>            InitializeComponent();<br>        }<br><br>        <span style="color:rgb(0,0,255)">private void</span> Command_CanExecute(<span style="color:rgb(0,0,255)">object</span> sender, <span style="color:rgb(51,153,102)">CanExecuteRoutedEventArgs</span> e)<br>        {<br>            e.CanExecute = <span style="color:rgb(0,0,255)">true</span>;<br>        }<br><br>        <span style="color:rgb(0,0,255)">private void</span> Save_Executed(<span style="color:rgb(0,0,255)">object</span> <span style="color:rgb(0,0,0)">sender</span>, <span style="color:rgb(51,153,102)">ExecutedRoutedEventArgs</span> e)<br>        {<br>            <span style="color:rgb(51,153,102)">MessageBox</span>.Show(<span style="color:rgb(128,0,0)">&quot;Save Executed&quot;</span>);<br><br>            System.Diagnostics.<span style="color:rgb(51,153,102)">Debug</span>.WriteLine(<span style="color:rgb(0,0,255)">this</span>.CommandBindings.Count);<br><br>            TestGrid.Children.Clear();<br><br>            System.Diagnostics.<span style="color:rgb(51,153,102)">Debug</span>.WriteLine(<span style="color:rgb(0,0,255)">this</span>.CommandBindings.Count);<br>        }<br><br>        <span style="color:rgb(0,0,255)">private void</span> Close_Executed(<span style="color:rgb(0,0,255)">object</span> sender, <span style="color:rgb(51,153,102)">ExecutedRoutedEventArgs</span> e)<br>        {<br>            <span style="color:rgb(51,153,102)">MessageBox</span>.Show(<span style="color:rgb(128,0,0)">&quot;Close Executed&quot;</span>);<br>        }<br><br>        <span style="color:rgb(0,0,255)">private void</span> Print_Executed(<span style="color:rgb(0,0,255)">object</span> sender, <span style="color:rgb(51,153,102)">ExecutedRoutedEventArgs</span> e)<br>        {<br>            <span style="color:rgb(51,153,102)">MessageBox</span>.Show(<span style="color:rgb(128,0,0)">&quot;Print Executed&quot;</span>);<br>        }<br>    }</p></div></div><br></div> <div style="text-align:left"><br></div>Wed, 16 Jan 2008 14:38:33 Z2008-01-16T14:38:33Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#9e55693d-2e60-4749-a394-06e5a449d0dahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#9e55693d-2e60-4749-a394-06e5a449d0dauncle hammyhttp://social.msdn.microsoft.com/Profile/en-US/?user=uncle%20hammyBizarre Command IssueOne further tidbit....<br><br>If the bindings are attached directly to the control (as opposed to the window), they remain in tact.  Though, this kind of defeats the purpose of a cental command if I have to attach a binding to every control, especially when the handlers will reside on a component other than which the control resides on.<br><br>If the above code is modified as follows, the Close button will properly remain enabled, even if the Save command is launched frm the &quot;content&quot; save button.<br><br> <div style="text-align:left"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Block</span></div> <div style="margin-left:40px"><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">  &lt;<span style="color:rgb(128,0,0)">ToolBarTray</span> <span style="color:rgb(255,0,0)">DockPanel.Dock</span><span style="color:rgb(0,0,255)">=&quot;Top&quot;</span>&gt;</span></span><br><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"></span></span></div> <p><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">            &lt;<span style="color:rgb(128,0,0)">ToolBar</span>&gt;<br>                &lt;<span style="color:rgb(128,0,0)">Button</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Save&quot;</span> <span style="color:rgb(255,0,0)">Content</span><span style="color:rgb(0,0,255)">=&quot;Save&quot;</span>/&gt;<br>                &lt;<span style="color:rgb(128,0,0)">Button</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Close&quot;</span> <span style="color:rgb(255,0,0)">Content</span><span style="color:rgb(0,0,255)">=&quot;Close&quot;</span>&gt;</span></span></p> <p style="margin-left:160px"><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">&lt;<span style="color:rgb(128,0,0)">Button.CommandBindings</span>&gt;</span></span></p> <p style="margin-left:200px"><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">&lt;<span style="color:rgb(128,0,0)">CommandBinding</span> <span style="color:rgb(255,0,0)">Command=</span><span style="color:rgb(0,0,255)">&quot;ApplicationCommands.Close&quot;</span> <span style="color:rgb(255,0,0)">CanExecute</span><span style="color:rgb(0,0,255)">=&quot;Command_CanExecute&quot;</span> <span style="color:rgb(255,0,0)">Executed</span><span style="color:rgb(0,0,255)">=&quot;Close_Executed&quot;</span>/&gt;</span></span><br><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"></span></span></p> <p style="margin-left:160px"><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">&lt;/<span style="color:rgb(128,0,0)">Button.CommandBindings</span>&gt;<br></span></span></p> <p style="margin-left:120px"><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"></span>  &lt;/<span style="color:rgb(128,0,0)">Button</span>&gt;<br><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody"></span></span></p> <p><span><span id="_ctl0_MainContent__ctl0_PostForm_ReplyBody">                &lt;<span style="color:rgb(128,0,0)">Button</span> <span style="color:rgb(255,0,0)">Command</span><span style="color:rgb(0,0,255)">=&quot;ApplicationCommands.Print&quot;</span> <span style="color:rgb(255,0,0)">Content</span><span style="color:rgb(0,0,255)">=&quot;Print&quot;/</span>&gt;<br>            &lt;/<span style="color:rgb(128,0,0)">ToolBar</span>&gt;<br>        &lt;/<span style="color:rgb(128,0,0)">ToolBarTray</span>&gt;</span></span></p></div></div><br></div>Wed, 16 Jan 2008 15:33:23 Z2008-01-16T15:33:23Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#7e0db640-5e1c-4339-b194-ecb65014d3c5http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#7e0db640-5e1c-4339-b194-ecb65014d3c5JBrophyhttp://social.msdn.microsoft.com/Profile/en-US/?user=JBrophyBizarre Command Issue<p align=left>I'm running into the same issue.  In my case however, the malfunctioning bindings are in menu bar items that are being dynamically created in code.  This is the best description of the problem I've seen, so I tried your workaround.  Since my items are created in code, I don't have to declare new CommandBindings for each item, I just copy all the Binding objects from the parent's CommandBindings collection each time I create a new menu item.  This allows the original bindings to still be defined in a separate component.</p> <p> </p> <p align=left>I'd still like to know if this is really a bug - is there a better way to do this - or will it be fixed in a future framework version?</p>Tue, 22 Apr 2008 19:13:18 Z2008-04-22T19:13:18Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#0fe17ea5-6ae1-43d7-9c2b-872a7f1b8e9fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f898ab7f-1282-48bb-a248-7ce1caa05cf6#0fe17ea5-6ae1-43d7-9c2b-872a7f1b8e9fkeith818http://social.msdn.microsoft.com/Profile/en-US/?user=keith818Bizarre Command Issue<p>You can resolve this by setting <span style="color:#ff0000"><span style="color:#ff0000">Focusable</span></span><span style="color:#0000ff"><span style="color:#0000ff">=&quot;True&quot; </span></span>in TestGrid. <br/><br/> <p><span style="color:#0000ff"><span style="color:#0000ff">&lt;</span></span><span style="color:#a31515"><span style="color:#a31515">Grid</span></span><span style="color:#ff0000"><span style="color:#ff0000"> x</span></span><span style="color:#0000ff"><span style="color:#0000ff">:</span></span><span style="color:#ff0000"><span style="color:#ff0000">Name</span></span><span style="color:#0000ff"><span style="color:#0000ff">=&quot;TestGrid&quot;</span></span><span style="color:#ff0000"><span style="color:#ff0000"> Focusable</span></span><span style="color:#0000ff"><span style="color:#0000ff">=&quot;True&quot;&gt;<br/></span></span><span style="color:#0000ff"><span style="color:#0000ff">    &lt;</span></span><span style="color:#a31515"><span style="color:#a31515">Button</span></span><span style="color:#ff0000"><span style="color:#ff0000"> Command</span></span><span style="color:#0000ff"><span style="color:#0000ff">=&quot;ApplicationCommands.Save&quot;</span></span><span style="color:#ff0000"><span style="color:#ff0000"> Content</span></span><span style="color:#0000ff"><span style="color:#0000ff">=&quot;Save&quot;/&gt;<br/></span></span><span style="color:#0000ff"><span style="color:#0000ff">&lt;/</span></span><span style="color:#a31515"><span style="color:#a31515">Grid</span></span><span style="color:#0000ff"><span style="color:#0000ff">&gt;</span></span></p> </p>Tue, 08 Sep 2009 15:59:51 Z2009-09-08T15:59:51Z