【发布时间】:2018-10-28 09:07:12
【问题描述】:
如何在以下代码中将enter key 分配给on_Click 事件?
为了说明这一点,我想在按下回车键时触发 on_Click() 方法中的代码。
同样,我正在使用 MVVMLight 框架。
视图模型
namespace MyApp.ViewModel
{
public class AppViewModel : ViewModelBase
{
public ICommand clickCommand { get; private set; }
public AppViewModel()
{
clickCommand = new RelayCommand(() => on_Click());
}
private void on_Click()
{
// button clicked
}
}
}
XAML
<Button x:Name="myButton"
Content="Click Me"
HorizontalAlignment="right"
Margin="0,84,72,0"
VerticalAlignment="Top"
Width="66" Height="25"
Command="{Binding clickCommand}" Foreground="#FFF2F5FC" BorderBrush="{x:Null}">
<Button.Background>
<SolidColorBrush Color="#FF3DA5DB"/>
</Button.Background>
</Button>
【问题讨论】:
-
非常感谢,我最终使用了来自链接
<UserControl.InputBindings> <KeyBinding Gesture="Enter" Command="{Binding someCommand}"/> </UserControl.InputBindings>的Nik's解决方案
标签: wpf mvvm data-binding mvvm-light