2013-09-08 3 views
1

Почему мой входной жест не работает в следующем?Жест ввода не работает wpf

public class CustomRoutedUICommand : RoutedUICommand 
    { 
     private static RoutedUICommand _doSomethingCommand = null; 
     static CustomRoutedUICommand() 
     { 
      InputGestureCollection myInputs = new InputGestureCollection(); 
      myInputs.Add(new KeyGesture(Key.G, ModifierKeys.Control | ModifierKeys.Shift)); 
      _doSomethingCommand = new RoutedUICommand("DoSomething", "DoSomething", typeof(CustomRoutedUICommand), myInputs); 
     } 
     public static RoutedUICommand DoSomethingCommand { get { return _doSomethingCommand; } } 
    } 

<Button Height="23" HorizontalAlignment="Left" 
       Command="{x:Static Control:CustomRoutedUICommand.DoSomethingCommand}" 
       CommandManager.CanExecute="Command_CanExecute" CommandManager.Executed="Command_Executed" 
       Content="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Command.Text}" 
       Margin="12,54,0,0" Name="Command" VerticalAlignment="Top" Width="Auto" Padding="2"/> 

private void Command_Executed(object sender, ExecutedRoutedEventArgs e) 
     { 
      MessageBox.Show("Hii"); 
     } 

ответ

1

В этом случае Command работать, нужно было элементом логической направленности, в противном случае он не будет работать. Вы можете указать KeyGesture в XAML путь:

<Button Height="23" Content="Test" Name="Command" VerticalAlignment="Top" 
     Command="{x:Static Control:CustomRoutedUICommand.DoSomethingCommand}" 
     CommandManager.Executed="Command_Executed" 
     CommandManager.CanExecute="Command_CanExecute"> 

    <Button.InputBindings> 
     <KeyBinding Command="{x:Static Control:CustomRoutedUICommand.DoSomethingCommand}" Gesture="CTRL+G" /> 
    </Button.InputBindings> 
</Button> 

Он работает, когда фокус будет, он может быть определен следующим образом:

Command.Focus(); 

Для того, чтобы ваше дело работать, вам нужно использовать CommandBindings как это:

XAML

<Window x:Class="InputGestureHelp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Control="clr-namespace:InputGestureHelp" 
     WindowStartupLocation="CenterScreen" 
     ContentRendered="Window_ContentRendered" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.CommandBindings> 
     <CommandBinding Command="{x:Static Control:CustomRoutedUICommand.DoSomethingCommand}" 
        Executed="Command_Executed" CanExecute="Command_CanExecute" /> 
    </Window.CommandBindings> 

    <Grid> 
     <Button Height="23" Content="Test" Name="TestButton" 
       VerticalAlignment="Top" 
       Command="{x:Static Control:CustomRoutedUICommand.DoSomethingCommand}" /> 
    </Grid> 
</Window> 

Code behind

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent();   
    } 

    private void Command_Executed(object sender, ExecutedRoutedEventArgs e) 
    { 
     MessageBox.Show("Hii"); 
    } 

    private void Command_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
    { 
     e.CanExecute = true; 
    } 

    private void Window_ContentRendered(object sender, EventArgs e) 
    { 
     TestButton.Focus(); 
    } 
} 

public class CustomRoutedUICommand : RoutedUICommand 
{ 
    private static RoutedUICommand _doSomethingCommand; 

    static CustomRoutedUICommand() 
    { 
     InputGestureCollection myInputs = new InputGestureCollection(); 

     myInputs.Add(new KeyGesture(Key.G, ModifierKeys.Control, "Ctrl + G")); 
     _doSomethingCommand = new RoutedUICommand("DoSomething", "DoSomething", typeof(CustomRoutedUICommand), myInputs); 
    } 

    public static RoutedUICommand DoSomethingCommand 
    { 
     get 
     { 
      return _doSomethingCommand; 
     } 
    } 
} 
Смежные вопросы