为什么我的输入手势不适用于以下内容?
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");
}
答案 0 :(得分:2)
在这种情况下,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;
}
}
}