Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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# KeyBinding-RelayCommand位于xaml.cs中_C#_Wpf_Binding - Fatal编程技术网

C# KeyBinding-RelayCommand位于xaml.cs中

C# KeyBinding-RelayCommand位于xaml.cs中,c#,wpf,binding,C#,Wpf,Binding,我将文本框绑定到ViewModel类。但是,button命令(它是一个RelayCommand,从ICommand扩展而来)绑定到UsersView.xaml.cs。在UsersView.xaml.cs构造函数中,我有以下内容: DataContext = UserVM; btnAdd.DataContext = this; 这就是我绑定按钮的方式-它可以工作 <Button Command="{Binding Add}" Content="Add user" /> 现在,我

我将文本框绑定到ViewModel类。但是,button命令(它是一个RelayCommand,从ICommand扩展而来)绑定到UsersView.xaml.cs。在UsersView.xaml.cs构造函数中,我有以下内容:

DataContext = UserVM;
btnAdd.DataContext = this;
这就是我绑定按钮的方式-它可以工作

<Button Command="{Binding Add}" Content="Add user" />

现在,我想为那个按钮添加keyperstation,但我不能为InputBindings设置DataContext,编译器在UsersVM类中找不到这个add命令

<UsersView.InputBindings>
    <KeyBinding Key="F10" Command="{Binding Add}" />
</UsersView.InputBindings>

我把它放在一个窗口上,这是我使用的代码

<Window
   x:Class="MVVMExample.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:myViewModels="clr-namespace:MVVMExample"
   Title="MainWindow"
   x:Name="MyMainWindow"
   Height="350"
   Width="525">

AddPersonCommand
是我的ViewModel中的ICommand。

您是否尝试过
命令=“{Binding Path=DataContext.Add}”
?这是在黑暗中拍摄,但可能会成功。是的,成功了!我设置了x:Name=“MyUsersView”并使用了以下绑定:。此绑定也适用于按钮命令绑定(从UsersView.xaml构造函数中删除了btnDataContext)。好的,如果它有效,并且你满意,那么把它标记为答案。
<Window.InputBindings>

    <KeyBinding
        Key="F10"
        Command="{Binding ElementName=MyMainWindow, Path=DataContext.AddPersonCommand}" />

</Window.InputBindings>