WPF(XAML):快捷键事件
我的XAML有问题。我有一个菜单组件,我想它会在快捷键工作了。我有XAML代码,但不起作用:WPF(XAML):快捷键事件,wpf,xaml,events,key,shortcut,Wpf,Xaml,Events,Key,Shortcut,我的XAML有问题。我有一个菜单组件,我想它会在快捷键工作了。我有XAML代码,但不起作用: <MenuItem Header="_New" Name="New" Click="New_Click" InputGestureText="Ctrl+N"> <MenuItem.InputBindings> <KeyBinding Key="N" Modifiers="control"/> </MenuItem.InputBindings>
<MenuItem Header="_New" Name="New" Click="New_Click" InputGestureText="Ctrl+N">
<MenuItem.InputBindings>
<KeyBinding Key="N" Modifiers="control"/>
</MenuItem.InputBindings>
</MenuItem>
解决办法是什么?New_Click事件起作用,但快捷键不起作用。使用
InputGestureText
只会将文本添加到菜单项中。您需要指定实际执行快捷方式时需要执行的操作。为此,您需要在ViewModel中创建一个ICommand,然后最好将该命令绑定到MenuItem.command
因此,生成的代码应该如下所示:
<MenuItem Header="_New" Name="New" Command="{Binding NewCommand}" InputGestureText="Ctrl+N">
请注意,卸载控件时可能需要删除或清除InputBinding,但我认为这将是您能得到的最接近的结果,更不用说我的原始答案了,答案是“您的问题”;您对其他信息的要求本身是一个单独的问题
另外,对实现InputBinding继承的类进行一些研究,使用
InputGestureText
只会将文本添加到菜单项中。您需要指定实际执行快捷方式时需要执行的操作。为此,您需要在ViewModel中创建一个ICommand,然后最好将该命令绑定到MenuItem.command
因此,生成的代码应该如下所示:
<MenuItem Header="_New" Name="New" Command="{Binding NewCommand}" InputGestureText="Ctrl+N">
请注意,卸载控件时可能需要删除或清除InputBinding,但我认为这将是您能得到的最接近的结果,更不用说我的原始答案了,答案是“您的问题”;您对其他信息的要求本身是一个单独的问题
另外,对实现InputBinding继承的类进行一些研究,可以在单击时应用快捷方式。我们需要使用命令绑定通过XAML实现此功能。 首先,需要将命令绑定到MenuItem,然后将同一命令绑定到keybinding。 以下是其工作代码:
<MenuItem Header="_New" Name="New" Command="{Binding NewCommand, Mode=TwoWay}" InputGestureText="Ctrl+N">
<MenuItem.InputBindings>
<KeyBinding Key="N" Modifiers="control" Command="{Binding NewCommand, Mode=TwoWay}"/>
</MenuItem.InputBindings>
</MenuItem>
using System;
using System.Windows;
using System.Windows.Input;
namespace NNN
{
/// <summary>This utility class translates ICommand calls to RoutedEventHandler calls</summary>
class c2e : ICommand
{
readonly RoutedEventHandler eh;
public c2e( RoutedEventHandler eh )
{
this.eh = eh;
}
public event EventHandler CanExecuteChanged;
bool ICommand.CanExecute( object parameter )
{
return true;
}
void ICommand.Execute( object parameter )
{
var a = new RoutedEventArgs();
this.eh( this, a );
}
}
static class Hotkey
{
/// <summary>Register event handler for hotkey</summary>
public static void registerHotkey( this Window wnd, Key key, ModifierKeys modifier, RoutedEventHandler handler )
{
ICommand cmd = new c2e( handler );
InputBinding ib = new InputBinding( cmd,new KeyGesture( key, modifier ) );
wnd.InputBindings.Add( ib );
}
}
}
以上代码将根据您的要求工作。如果您对此有任何疑问,请告诉我。
谢谢您单击即可应用快捷方式。我们需要使用命令绑定通过XAML实现此功能。 首先,需要将命令绑定到MenuItem,然后将同一命令绑定到keybinding。 以下是其工作代码:
<MenuItem Header="_New" Name="New" Command="{Binding NewCommand, Mode=TwoWay}" InputGestureText="Ctrl+N">
<MenuItem.InputBindings>
<KeyBinding Key="N" Modifiers="control" Command="{Binding NewCommand, Mode=TwoWay}"/>
</MenuItem.InputBindings>
</MenuItem>
using System;
using System.Windows;
using System.Windows.Input;
namespace NNN
{
/// <summary>This utility class translates ICommand calls to RoutedEventHandler calls</summary>
class c2e : ICommand
{
readonly RoutedEventHandler eh;
public c2e( RoutedEventHandler eh )
{
this.eh = eh;
}
public event EventHandler CanExecuteChanged;
bool ICommand.CanExecute( object parameter )
{
return true;
}
void ICommand.Execute( object parameter )
{
var a = new RoutedEventArgs();
this.eh( this, a );
}
}
static class Hotkey
{
/// <summary>Register event handler for hotkey</summary>
public static void registerHotkey( this Window wnd, Key key, ModifierKeys modifier, RoutedEventHandler handler )
{
ICommand cmd = new c2e( handler );
InputBinding ib = new InputBinding( cmd,new KeyGesture( key, modifier ) );
wnd.InputBindings.Add( ib );
}
}
}
以上代码将根据您的要求工作。如果您对此有任何疑问,请告诉我。
谢谢你这是我的实用课程:
<MenuItem Header="_New" Name="New" Command="{Binding NewCommand, Mode=TwoWay}" InputGestureText="Ctrl+N">
<MenuItem.InputBindings>
<KeyBinding Key="N" Modifiers="control" Command="{Binding NewCommand, Mode=TwoWay}"/>
</MenuItem.InputBindings>
</MenuItem>
using System;
using System.Windows;
using System.Windows.Input;
namespace NNN
{
/// <summary>This utility class translates ICommand calls to RoutedEventHandler calls</summary>
class c2e : ICommand
{
readonly RoutedEventHandler eh;
public c2e( RoutedEventHandler eh )
{
this.eh = eh;
}
public event EventHandler CanExecuteChanged;
bool ICommand.CanExecute( object parameter )
{
return true;
}
void ICommand.Execute( object parameter )
{
var a = new RoutedEventArgs();
this.eh( this, a );
}
}
static class Hotkey
{
/// <summary>Register event handler for hotkey</summary>
public static void registerHotkey( this Window wnd, Key key, ModifierKeys modifier, RoutedEventHandler handler )
{
ICommand cmd = new c2e( handler );
InputBinding ib = new InputBinding( cmd,new KeyGesture( key, modifier ) );
wnd.InputBindings.Add( ib );
}
}
}
下面是我的实用程序类:
<MenuItem Header="_New" Name="New" Command="{Binding NewCommand, Mode=TwoWay}" InputGestureText="Ctrl+N">
<MenuItem.InputBindings>
<KeyBinding Key="N" Modifiers="control" Command="{Binding NewCommand, Mode=TwoWay}"/>
</MenuItem.InputBindings>
</MenuItem>
using System;
using System.Windows;
using System.Windows.Input;
namespace NNN
{
/// <summary>This utility class translates ICommand calls to RoutedEventHandler calls</summary>
class c2e : ICommand
{
readonly RoutedEventHandler eh;
public c2e( RoutedEventHandler eh )
{
this.eh = eh;
}
public event EventHandler CanExecuteChanged;
bool ICommand.CanExecute( object parameter )
{
return true;
}
void ICommand.Execute( object parameter )
{
var a = new RoutedEventArgs();
this.eh( this, a );
}
}
static class Hotkey
{
/// <summary>Register event handler for hotkey</summary>
public static void registerHotkey( this Window wnd, Key key, ModifierKeys modifier, RoutedEventHandler handler )
{
ICommand cmd = new c2e( handler );
InputBinding ib = new InputBinding( cmd,new KeyGesture( key, modifier ) );
wnd.InputBindings.Add( ib );
}
}
}
由于上述解决方案中没有一个对我有效,我想在此建议另一个解决方案,使用 XAML
CS
公共部分类主窗口:窗口
{
专用静态ICommand_clickCommand;
公共静态ICommand
MenuItemClickCommand=>\u clickCommand??
(_clickCommand=newroutedUICommand(
文本:“选项”,
名称:“MenuItemClickCommand”,
ownerType:typeof(主窗口),
输入手势:新的输入手势集合(
输入手势:新的输入手势[]{
新按键手势(按键N,修改键控件)
})));
公共主窗口()
{
初始化组件();
焦点();
}
已单击菜单项上的私有void(对象发送方,RoutedEventArgs e)
{
Show(“上下文菜单项已单击!”);
}
私有void CanExecute(对象发送方,CanExecuteRoutedEventArgs e)
{
e、 CanExecute=true;//或其他逻辑
}
}
由于上述解决方案中没有一种适合我,我想在此建议另一种解决方案,使用
XAML
CS
公共部分类主窗口:窗口
{
专用静态ICommand_clickCommand;
公共静态ICommand
MenuItemClickCommand=>\u clickCommand??
(_clickCommand=newroutedUICommand(
文本:“选项”,
名称:“MenuItemClickCommand”,
ownerType:typeof(主窗口),
输入手势:新的输入手势集合(
输入手势:新的输入手势[]{
新按键手势(按键N,修改键控件)
})));
公共主窗口()
{
初始化组件();
焦点();
}
已单击菜单项上的私有void(对象发送方,RoutedEventArgs e)
{
Show(“上下文菜单项已单击!”);
}
私有void CanExecute(对象发送方,CanExecuteRoutedEventArgs e)
{
e、 CanExecute=true;//或其他逻辑
}
}