none
MenuItem And Command Binding Issue RRS feed

  • Question

  • I have a simple app a window, with a  menu, one menuItem link to a command. I have a button that is link to the same command.
    when the menu or button is clicked it add a dummy user control to the stackpanel. it register the unload event has well.
    the dummy control contains only one button. when you click it, the usercontrol get remove from the panel through this line: ((StackPanel)(this.Parent)).Children.Remove(this);
    From there here is the problem
    menu is disabled , and Button is still enabled, why? I would like to keep the menu option enable always.
    here is the code for the app:
    <Window x:Class="Commands.TestCommand" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="TestCommand"  
        Name="MyWindow"
      <Window.CommandBindings> 
        <CommandBinding Command="ApplicationCommands.New" 
          Executed="NewCommand" /> 
      </Window.CommandBindings> 
      <StackPanel Name="Container"
        <Menu> 
          <MenuItem Name="TopMenu" Header="File"
            <MenuItem Name="menuItemNew" Command="New"></MenuItem> 
             </MenuItem> 
        </Menu> 
        <Button Command="New">New</Button> 
        </StackPanel> 
    </Window> 
    using System.Windows.Input; 
     
    namespace Commands 
        /// <summary> 
        /// Interaction logic for TestCommand.xaml 
        /// </summary> 
     
        public partial class TestCommand : System.Windows.Window 
        { 
            public TestCommand() 
            { 
                InitializeComponent(); 
            } 
     
            private void NewCommand(object sender, ExecutedRoutedEventArgs e) 
            { 
                DummyControl dummy = new DummyControl(); 
                Container.Children.Add(dummy); 
            } 
        } 
    }






    Here is the code for the dummy user control:
    <UserControl x:Class="Commands.DummyControl" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        <Grid> 
            <Button Height="23" Name="BtnClose" VerticalAlignment="Bottom" Click="Close_Click">Close Me !</Button> 
        </Grid> 
    </UserControl> 

    using System.Windows; 
    using System.Windows.Controls; 
     
    namespace Commands 
        /// <summary> 
        /// Interaction logic for DummyControl.xaml 
        /// </summary> 
        public partial class DummyControl : UserControl 
        { 
            #region CTORS 
            public DummyControl() 
            { 
                InitializeComponent(); 
            } 
            #endregion 
     
            private void Close_Click(object sender, RoutedEventArgs e) 
            { 
                ((StackPanel)(this.Parent)).Children.Remove(this); 
            } 
        } 




    This is a very simple application, but show the issue.
    Step to reproduce:
    - click on the button new -> usercontrol appears, click on the "click me" -> user control disappears and button new is still enable but menu is disabled (it shouldn't - I would like to see it staying enabled)
    second way: click on the menu item new -> usercontrol appears, click on the "click me" -> user control disappears and button new is still enable but menu is disabled


    Any help would be really appreciate.




    Wednesday, July 16, 2008 1:24 AM

Answers

  • I found the solution. It seems to be a scope issue.
    if in the main program I replace
    <Window.CommandBindings> 
        <CommandBinding Command="ApplicationCommands.New" Executed="NewCommand" /> 
      </Window.CommandBindings> 
     
    by 
     
    <Menu> 
      <Menu.CommandBindings> 
        <CommandBinding Command="ApplicationCommands.New"  Executed="NewCommand" /> 
      </Menu.CommandBindings> 
       <MenuItem Name="menuItemNew" Command="New"></MenuItem> 
    </Menu> 

    it solves the issue.
    so there is no need to work around with the focus.  Now I would like to understand why it behave this way. is it expected, is it a bug? shouldn't an item define at an upper level apply at a lower level? I you have a technical answer I will appreciate.





    • Marked as answer by pipobingo Wednesday, July 16, 2008 8:13 AM
    • Marked as answer by pipobingo Wednesday, July 16, 2008 8:13 AM
    • Marked as answer by pipobingo Wednesday, July 16, 2008 8:13 AM
    Wednesday, July 16, 2008 8:12 AM

All replies

  • I am not particularly sure why but for some reason it seems like Menu wants to make sure that some element has focus in the application before allowing MenuItems bound to commands to be executed.  If you set your StackPanel so that it is Focusable and you also change your Close_Click event handler so that it sets focus back to the application after removing itself, then this problem will go away:

            private void Close_Click(object sender, RoutedEventArgs e)  
            {  
                StackPanel stackPanel;  
     
                if((stackPanel = this.Parent as StackPanel) != null)  
                {  
                    stackPanel.Children.Remove(this);  
                    stackPanel.Focus();  
                }  
                  
            }   

    Hope this helps,
    Wells



    Wells Caughey | Magenic Technologies
    Wednesday, July 16, 2008 3:26 AM
  • Hi,
    It seems to solve the problem partially... if you click on the button new and then close the "click me" button the menu stay enabled.But if straight you click on the new menu item then close the "click me" the menu unfortunately get disabled... any clue?
    Wednesday, July 16, 2008 3:58 AM
  • I found the solution. It seems to be a scope issue.
    if in the main program I replace
    <Window.CommandBindings> 
        <CommandBinding Command="ApplicationCommands.New" Executed="NewCommand" /> 
      </Window.CommandBindings> 
     
    by 
     
    <Menu> 
      <Menu.CommandBindings> 
        <CommandBinding Command="ApplicationCommands.New"  Executed="NewCommand" /> 
      </Menu.CommandBindings> 
       <MenuItem Name="menuItemNew" Command="New"></MenuItem> 
    </Menu> 

    it solves the issue.
    so there is no need to work around with the focus.  Now I would like to understand why it behave this way. is it expected, is it a bug? shouldn't an item define at an upper level apply at a lower level? I you have a technical answer I will appreciate.





    • Marked as answer by pipobingo Wednesday, July 16, 2008 8:13 AM
    • Marked as answer by pipobingo Wednesday, July 16, 2008 8:13 AM
    • Marked as answer by pipobingo Wednesday, July 16, 2008 8:13 AM
    Wednesday, July 16, 2008 8:12 AM