2010-07-06 38 views
0

我遇到了使用熱鍵下拉文件菜單的問題Alt + F。我已經成功地刪除它,如果Alt被按下並釋放後跟一個F,打開菜單,但按alt不會做的伎倆。這是我正在使用的代碼。爲MenuItem(文件)分配熱鍵以刪除它

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

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

使用的文件的開頭下劃線使菜單的打開,首先按下和釋放Alt鍵然後˚F

我會以某種方式所需的文件菜單下降當兩個按鍵被擠壓在一起..

這是我剛纔用來分配熱鍵

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) 
{ } 
代碼210

我只想要將一些代碼放在{}括號內。

+0

提示:使用代碼格式化,使您的問題更具可讀性。 – Dennis 2010-07-06 12:21:58

回答

0

如果您在ControlTemplate中查看WPF中的MenuItem,您將看到它使用PopUp基元和ItemsControl來顯示菜單。

要手動觸發MenuItem打開,您需要設置PopUp.IsOpen = true;

現在......我不記得IsOpen屬性是否暴露在MenuItem上,因此您可能需要遍歷可視化樹以查找對其的引用。

MyVisualTreeHelper這是使用我寫過的包裝器(就像我之前的許多其他人一樣)快速走過視覺樹。部分內容如下。

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

感謝丹尼斯..但我有一種感覺,還有一些其他問題..謝謝反正.. – Amal 2010-07-07 06:21:31

+0

道歉,我誤讀/理解這個問題。 – Dennis 2010-07-07 10:16:57