C# 在WPF中创建密钥绑定
我需要为窗口创建输入绑定C# 在WPF中创建密钥绑定,c#,wpf,xaml,C#,Wpf,Xaml,我需要为窗口创建输入绑定 public class MainWindow : Window { public MainWindow() { SomeCommand = ??? () => OnAction(); } public ICommand SomeCommand { get; private set; } public void OnAction() { SomeControl.DoSomethin
public class MainWindow : Window
{
public MainWindow()
{
SomeCommand = ??? () => OnAction();
}
public ICommand SomeCommand { get; private set; }
public void OnAction()
{
SomeControl.DoSomething();
}
}
如果我使用some
CustomCommand:ICommand
初始化SomeCommand,它不会启动SomeCommand
属性getter永远不会被调用。您必须创建自己的命令
实现ICommand
接口,并使用该命令的实例初始化SomeCommand
现在您必须将窗口的DataContext
设置为self,以使命令
绑定
工作:
public MainWindow()
{
InitializeComponents();
DataContext = this;
SomeCommand = MyCommand() => OnAction();
}
或者,您必须将您的绑定更新为
<Window>
<Window.InputBindings>
<KeyBinding Command="{Binding SomeCommand, RelativeSource={RelativeSource Self}}" Key="F5"></KeyBinding>
</Window.InputBindings>
</Window>
对于您的案例,使用MVVM模式的最佳方式
XAML:
在视图模型中:
public class MyViewModel
{
private ICommand someCommand;
public ICommand SomeCommand
{
get
{
return someCommand
?? (someCommand = new ActionCommand(() =>
{
MessageBox.Show("SomeCommand");
}));
}
}
}
然后需要实现ICommand
。
这堂课很简单,很有帮助
public class ActionCommand : ICommand
{
private readonly Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
对于修改器(组合键):
可能太晚了,但这里是最简单、最短的解决方案
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.S)
{
// Call your method here
}
}
<Window x:Class="Test.MainWindow" KeyDown="Window_KeyDown" >
private void Window\u KeyDown(对象发送方,KeyEventArgs e)
{
if(Keyboard.Modifiers==ModifierKeys.Control&&e.Key==Key.S)
{
//在这里调用您的方法
}
}
这就是我在项目中解决这个问题的方法:
<Window x:Class="MyNamespace.MyView"
(...)
xmlns:local="clr-namespace:MyNameSpace"
(...)
<Grid>
<Grid.InputBindings>
<KeyBinding Key="R" Command="{Binding ReportCommand,
RelativeSource={RelativeSource AncestorType=local:MyView}}" />
(...)
有没有快速简单的方法来运行一个方法void buttonPress(){MessageBox.Show(“它有效”)
在任何特定的组合键上,例如Ctrl+A?我在谷歌搜索了20分钟,我找不到一个简单的例子来做这件事。@MKII不要忘记将DataContext=this;
添加到Mainwindow
方法中它不起作用。e.key
有LeftCtrl
或RightCtrl
,因为它不起作用将在按下修改键后立即触发
public class ActionCommand : ICommand
{
private readonly Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.S)
{
// Call your method here
}
}
<Window x:Class="Test.MainWindow" KeyDown="Window_KeyDown" >
<Window x:Class="MyNamespace.MyView"
(...)
xmlns:local="clr-namespace:MyNameSpace"
(...)
<Grid>
<Grid.InputBindings>
<KeyBinding Key="R" Command="{Binding ReportCommand,
RelativeSource={RelativeSource AncestorType=local:MyView}}" />
(...)