2011-03-16 20 views
5

MenuItem için Klavye kısayolları uygulamak istiyorum. Ben kod aşağıda kullanmış:MenuItem.InputGestureText ayarı neden giriş hareketini gerçekleştirdiğimde MenuItem'in etkinleşmesine neden oluyor?

<MenuItem Header="_New" InputGestureText="CTRL+N" Click="NewMenu_Click"> 
    <MenuItem.Icon> 
     <Image Source= "Images\NEW.PNG" Width="25" Height="28" /> 
    </MenuItem.Icon> 
</MenuItem>` 

Ama CTRL+N basıldığında InputGestureText mülkiyet yanıt vermiyor. Visual Studio Express Edition 2010 kullanıyorum. Burada bir şey eksik miyim?

cevap

9

Bu özellik için belgelerinde oldukça açıktır:

Bu özellik menü öğesiyle giriş jest ilişkilendirme değil; sadece menü öğesine metin ekler. uygulamasının, işlemi gerçekleştirmek için kullanıcının girişini kullanması gerekir. komutunu bir menü öğesiyle ilişkilendirmeye ilişkin bilgi için bkz. Command.

4

Bunu yapmanın en iyi yolu, bir Command yapmak ve bu komutla InputGesture ilişkilendirmek geçerli:

public static class Commands 
{ 
    public static readonly RoutedCommand CreateNew = new RoutedCommand("New", typeof(Commands)); 

    static Commands() 
    { 
     SomeCommand.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control)); 
    } 
} 

... 

// Wherever you want to create the MenuItem. "local" should be the namespace that 
// you delcared "Commands" in. 
<MenuItem Header="_New" Command="{x:Static local:Commands.CreateNew}"> 

... 

// Wherever you want to process the command. I am assuming you want to do it in your 
// main window, but you can do it anywhere in the route between your main window and 
// the menu item. 
<Window.CommandBindings> 
    <CommandBinding Command="{x:Static local:Commands.CreateNew}"> Executed="CreateNew_Executed" /> 
</Window.CommandBindings> 

... 

// In the code behind for your main window (or whichever file you put the above XAML in) 
private void CreateNew(object sender, ExecutedRoutedEventArgs e) 
{ 
    ... 
} 

Eğer gerçekten sadece bir "Yeni" komutunu istiyorsanız, RoutedCommand oluştururken atlayabilirsiniz bu komut sizin için zaten oluşturulur çünkü InputGesture: komutları ve bağlamaları içermeyen

<MenuItem Header="_New" Command="New"> 

... 

<Window.CommandBindings> 
    <CommandBinding Command="New" Executed="New_Executed" /> 
</Window.CommandBindings> 

... 

private void New_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    ... 
} 
2

bir çözüm sahibi olmak geçersiz kılmak için Pencerenin OnKeyDown yöntemini kullanın ve klavye olayına uyan bir KeyGesture olan bir menü öğesini arayın. İşte

Pencere onkeydown geçersiz kılma kodudur:

protected override void OnKeyDown(KeyEventArgs e) 
{ 
    base.OnKeyDown(e); 

    // here I suppose the window's menu is named "MainMenu" 
    MainMenu.RaiseMenuItemClickOnKeyGesture(e); 
} 

Ve burada klavye olayı bir menü öğesi eşleşen yarar kodudur:

public static void RaiseMenuItemClickOnKeyGesture(this ItemsControl control, KeyEventArgs args) => RaiseMenuItemClickOnKeyGesture(control, args, true); 
    public static void RaiseMenuItemClickOnKeyGesture(this ItemsControl control, KeyEventArgs args, bool throwOnError) 
    { 
     if (args == null) 
      throw new ArgumentNullException(nameof(args)); 

     if (control == null) 
      return; 

     var kgc = new KeyGestureConverter(); 
     foreach (var item in control.Items.OfType<MenuItem>()) 
     { 
      if (!string.IsNullOrWhiteSpace(item.InputGestureText)) 
      { 
       KeyGesture gesture = null; 
       if (throwOnError) 
       { 
        gesture = kgc.ConvertFrom(item.InputGestureText) as KeyGesture; 
       } 
       else 
       { 
        try 
        { 
         gesture = kgc.ConvertFrom(item.InputGestureText) as KeyGesture; 
        } 
        catch 
        { 
        } 
       } 

       if (gesture != null && gesture.Matches(null, args)) 
       { 
        item.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent)); 
        args.Handled = true; 
        return; 
       } 
      } 

      RaiseMenuItemClickOnKeyGesture(item, args, throwOnError); 
      if (args.Handled) 
       return; 
     } 
    } 
İlgili konular