Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中创建密钥绑定_C#_Wpf_Xaml - Fatal编程技术网

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}}" />
    (...)