Hi,
I'm trying to make my WPF app to show me a message anytime that I press 'Enter',
This is what I want to Achieve anytime that the app is working whenever I press Enter It Gives me a message or throw an exception so I can see the It's reading my key press.
Below I put my code (As I've searched so many solutions for my problem within the last week It's very similar to the codes I've found about this problem on SO, MSDN, Channel9 and so on)
My XAML code:
<Window.InputBindings>
<KeyBinding Command="{Binding throwExceptionTest}" Key="Enter"/>
</Window.InputBindings>
MainWindow code:
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
ViewModel code:
public class MyViewModel
{
private ICommand throwExceptionTest;
public ICommand ThrowExceptionTest => throwExceptionTest?? (throwExceptionTest = new ActionCommand(() =>
{
MessageBox.Show("ThrowExceptionTest");
}));
}
The ICommand code:
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;
}
The Other problem I have here As I'm a beginner in WPF is that I don't know how I can trace this keypress event.
Any help, hint, comment could be helpful.
In advance, I appreciate that you spend your time reading this question and helping me.
Thanks,
What I have tried:
I searched the past week over SO, MSDN, Channel9, YouTube and yet The problem still persists.