1

为什么我的输入手势在下面不起作用?

 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");
        }
4

1 回答 1

3

在这种情况下,Command要工作,需要有一个元素逻辑焦点,否则将无法工作。您可以指定KeyGestureXAML 方式:

<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; 
        }
    }
}
于 2013-09-08T15:20:13.697 回答