locked
Programmatically click menu item. RRS feed

  • Question

  • Hey guys,

    seems to be really simple, but it's not, as least it's not working for me.
    As I know, WPF have 2 ways how to programmatically raise some event.

    1.  myButtons.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

    2.   ButtonAutomationPeer peer =

      new ButtonAutomationPeer(myButton);

     

                IInvokeProvider invokeProv =

                  peer.GetPattern(PatternInterface.Invoke)

                  as IInvokeProvider;

                invokeProv.Invoke();

    These events works great with simple button, but they makes nothing when I'm trying to do invoke menu item click event.
     1.  myMenu.myMenuItem.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

    2.   MenuItemAutomationPeerpeer =

      new MenuItemAutomationPeer(myMenu.myMenuItem);

                IInvokeProvider invokeProv =

                  peer.GetPattern(PatternInterface.Invoke)

                  as IInvokeProvider;

                invokeProv.Invoke();

    Any ideas why it's not working for me?

    Many thanks! 

    Tuesday, May 24, 2011 9:34 PM

Answers

  • I tried to replicate your issue but i couldnt, i had to change this line so i could test it, but it seems to do the job.

    myMenu.myMenuItem.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

    changed to...

    ((MenuItem)myMenu.Items[0]).RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

    or

    myMenuItem.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

     

    in both cases i get the click event executing, i am using .NET 4.0. Maybe there is something missing in your example?

    • Proposed as answer by Jie Bao Thursday, May 26, 2011 1:19 AM
    • Marked as answer by Jie Bao Wednesday, June 1, 2011 5:01 PM
    Tuesday, May 24, 2011 11:34 PM
  • Agree with sehlers, I do not think you could access the sub menu item from the parent by its name directly. Please use the index to get the sub menu item, and raise its event.
    And for the 2nd way, I checked the UISpy (one tool in the Windows SDK http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505&displaylang=en ) The MenuItem which has the sub MenuItems only supports the PatternInterface.ExpandCollapse, and the MenuItem which does not have the sub MenuItems can support PatternInterface.Invoke. Please refer to: http://msdn.microsoft.com/en-us/library/ms750574.aspx
     
    So we only can use the MenuItemAutomationPeer to control the MenuItem which has the sub MenuItems to Expand and Collapse:
       MenuItemAutomationPeer peer = new MenuItemAutomationPeer(myMenu.Items[0] as MenuItem);
    
       IExpandCollapseProvider ExpandCollapseProv =
        peer.GetPattern(PatternInterface.ExpandCollapse) as IExpandCollapseProvider;
       ExpandCollapseProv.Expand();
    
    and for non-sub MenuItems:
       MenuItemAutomationPeer peer = new MenuItemAutomationPeer(myMenu.Items[0] as MenuItem);
    
       IInvokeProvider invokeProv =
        peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
       invokeProv.Invoke();
    

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jie Bao Thursday, May 26, 2011 1:19 AM
    • Marked as answer by Jie Bao Wednesday, June 1, 2011 5:01 PM
    Thursday, May 26, 2011 1:18 AM

All replies

  • I tried to replicate your issue but i couldnt, i had to change this line so i could test it, but it seems to do the job.

    myMenu.myMenuItem.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

    changed to...

    ((MenuItem)myMenu.Items[0]).RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

    or

    myMenuItem.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));

     

    in both cases i get the click event executing, i am using .NET 4.0. Maybe there is something missing in your example?

    • Proposed as answer by Jie Bao Thursday, May 26, 2011 1:19 AM
    • Marked as answer by Jie Bao Wednesday, June 1, 2011 5:01 PM
    Tuesday, May 24, 2011 11:34 PM
  • Agree with sehlers, I do not think you could access the sub menu item from the parent by its name directly. Please use the index to get the sub menu item, and raise its event.
    And for the 2nd way, I checked the UISpy (one tool in the Windows SDK http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505&displaylang=en ) The MenuItem which has the sub MenuItems only supports the PatternInterface.ExpandCollapse, and the MenuItem which does not have the sub MenuItems can support PatternInterface.Invoke. Please refer to: http://msdn.microsoft.com/en-us/library/ms750574.aspx
     
    So we only can use the MenuItemAutomationPeer to control the MenuItem which has the sub MenuItems to Expand and Collapse:
       MenuItemAutomationPeer peer = new MenuItemAutomationPeer(myMenu.Items[0] as MenuItem);
    
       IExpandCollapseProvider ExpandCollapseProv =
        peer.GetPattern(PatternInterface.ExpandCollapse) as IExpandCollapseProvider;
       ExpandCollapseProv.Expand();
    
    and for non-sub MenuItems:
       MenuItemAutomationPeer peer = new MenuItemAutomationPeer(myMenu.Items[0] as MenuItem);
    
       IInvokeProvider invokeProv =
        peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
       invokeProv.Invoke();
    

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jie Bao Thursday, May 26, 2011 1:19 AM
    • Marked as answer by Jie Bao Wednesday, June 1, 2011 5:01 PM
    Thursday, May 26, 2011 1:18 AM