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 に答える