开发者

Assign Hotkey to a MenuItem (File) to drop it

开发者 https://www.devze.com 2023-01-05 16:43 出处:网络
I am having an issue dropping down the File menu using the hotkey Alt+F. I have successfully been able to drop it if Alt is pressed and released followed by an F, opens the menu but pressing f with al

I am having an issue dropping down the File menu using the hotkey Alt+F. I have successfully been able to drop it if Alt is pressed and released followed by an F, opens the menu but pressing f with alt doesnt do the trick. This is the code that I am using.

< Menu Name="File_Menu" Background="LightGray">

< MenuItem Header="_File" Background="LightGray" Name="File_FileMenu" > 

using the underscore at the begining of File enables the opening of the menu by first pressing and releasing Alt and then F

I would want somehow the file menu to drop when both the keys are pressed together..

this is the code I have used earlier to assign hotkey

KeyGesture keyGestureAltF = new KeyGesture (Key.F, ModifierKeys.Alt);
CommandBinding commandAltFBinding = new CommandBinding (CustomCommands.commandAltF, CommandBinding_FileMenu);
CustomCommands.commandAltF.I开发者_开发技巧nputGestures.Add (keyGestureAltF);
this.CommandBindings.Add (commandAltFBinding);

private void CommandBinding_FileMenu(object sender,ExecutedRoutedEventArgs e)
{ }

I would just want some code that is to be placed inside {} braces.


If you look at the ControlTemplate for a MenuItem in WPF you will see that it uses a PopUp primitive with a ItemsControl to display the menu.

To manually trigger the MenuItem to open you need to set the PopUp.IsOpen = true;.

Now... I cannot remember if the IsOpen property is exposed on the MenuItem, you may need to walk the visual tree to find a reference to it.

The MyVisualTreeHelper that is use a wrapper that I've written (like so many others before me) to quickly walk the visual tree. Part of it is below.

public static class MyVisualTreeHelper
{
  static bool AlwaysTrue<T>(T obj) { return true; }

  /// <summary>
  /// Finds a parent of a given item on the visual tree. If the element is a ContentElement or FrameworkElement 
  /// it will use the logical tree to jump the gap.
  /// If not matching item can be found, a null reference is returned.
  /// </summary>
  /// <typeparam name="T">The type of the element to be found</typeparam>
  /// <param name="child">A direct or indirect child of the wanted item.</param>
  /// <returns>The first parent item that matches the submitted type parameter. If not matching item can be found, a null reference is returned.</returns>
  public static T FindParent<T>(DependencyObject child) where T : DependencyObject
  {
    return FindParent<T>(child, AlwaysTrue<T>);
  }

  public static T FindParent<T>(DependencyObject child, Predicate<T> predicate) where T : DependencyObject
  {
    DependencyObject parent = GetParent(child);
    if (parent == null)
      return null;

    // check if the parent matches the type and predicate we're looking for
    if ((parent is T) && (predicate((T)parent)))
      return parent as T;
    else
      return FindParent<T>(parent);
  }

  static DependencyObject GetParent(DependencyObject child)
  {
    DependencyObject parent = null;
    if (child is Visual || child is Visual3D)
      parent = VisualTreeHelper.GetParent(child);

    // if fails to find a parent via the visual tree, try to logical tree.
    return parent ?? LogicalTreeHelper.GetParent(child);
  }
}

HTH,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号