2

I desire following behavior:

  • Pressing Ctrl + 1 causes selecting first item from list
  • Ctrl + 2 selects first two elements
  • And so on.

Currently I'm handling pressed keys by RoutedEvent, Window.CommandBindings and InputGesture. It works fine, but I have ten almost same commands (1,2,3...9,0). Is there better way? Eg, sending pressed key to CommandBinding.Executed

Sample of my code:

//XAML
<Window.CommandBindings>
        <CommandBinding Command="{x:Static local:CustomCommands.SelectOne}" Executed="cmdSelectOne_Executed"  />
//CustomCommands.cs
public static class CustomCommands
{
    public static RoutedCommand SelectOne = new RoutedCommand();
            //...

    static CustomCommands()
    {
        omg.InputGestures.Add(new KeyGesture(Key.D1, ModifierKeys.Control, "Ctrl + 1"));
            //...

    //MainWindow.xaml.cs
    private void cmdSelectOne_Executed(object sender, ExecutedRoutedEventArgs e)
mbednarski
  • 758
  • 1
  • 9
  • 17
  • 1
    Why don't you use a `KeyDown` event instead of a `Command` so you have access to `e.Key`? – Rachel May 06 '13 at 13:50

0 Answers0