c# – KeyBinding – RelayCommand位于xaml.cs中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – KeyBinding – RelayCommand位于xaml.cs中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1067字,纯文字阅读大概需要2分钟。
内容图文
![c# – KeyBinding – RelayCommand位于xaml.cs中](/upload/InfoBanner/zyjiaocheng/776/96ceb1d49b724d54bff4602061fa4b20.jpg)
我将我的文本框绑定到ViewModel类.但是,按钮命令(它是一个RelayCommand,从ICommand扩展)我绑定到UsersView.xaml.cs.在UsersView.xaml.cs构造函数中,我有:
DataContext = UserVM;
btnAdd.DataContext = this;
这是我绑定按钮的方式 – 它的工作原理.
<Button Command="{Binding Add}" Content="Add user" />
现在,我想为该按钮添加KeyGesture,但我无法为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">
请注意,我设置了Window的x.Name.然后在我的KeyBinding中,我这样做了……
<Window.InputBindings>
<KeyBinding
Key="F10"
Command="{Binding ElementName=MyMainWindow, Path=DataContext.AddPersonCommand}" />
</Window.InputBindings>
AddPersonCommand是我的ViewModel的ICommand.
内容总结
以上是互联网集市为您收集整理的c# – KeyBinding – RelayCommand位于xaml.cs中全部内容,希望文章能够帮你解决c# – KeyBinding – RelayCommand位于xaml.cs中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。