tags:

views:

13

answers:

2

Hi, 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.InputGestures.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.

A: 

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,

Dennis Roche
Thanks Dennis.. but i have a feeling there is some other issue.. thanks anyways..
Amal
Apologies, I misread/understood the question.
Dennis Roche
A: 

The above code written by me is perfectly fine.. i tired it in a new project and it works fine there.. but i guess due to some dependency it doesnt work in the project i m trying it in due to some dependency..

Amal