Value cannot be null CommandBinding

I just started with WPF a few days ago and came up with a problem that I don't understand.

I got the following error:

The value cannot be null. Parameter Name: Value

The error is here:

<Window.CommandBindings> <CommandBinding Command="self:CustomCommands.Exit" Executed="ExitCommand_Executed" CanExecute="ExitCommand_CanExecute"/> </Window.CommandBindings> 

Of course, I set the xmlns:self="clr-namespace:PrintMonitor" .

Code:

 namespace PrintMonitor { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if(e != null) e.CanExecute = true; } private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e) { Application.Current.Shutdown(); } } public static class CustomCommands { public static readonly RoutedUICommand Exit = new RoutedUICommand ( "Beenden", "Exit", typeof(CustomCommands), new InputGestureCollection() { new KeyGesture(Key.F4, ModifierKeys.Alt) } ); } } 

So why this error occurs if I use a user command, but not if I use, for example. Command="ApplicationCommands.New" and how can I fix this error?

Code is part of this guide .

+5
source share
1 answer

You might need to install custom user_commands,

and for MainWindow DataContext to be CustomCommands

  public class CustomCommands { public static readonly RoutedUICommand Exit = new RoutedUICommand ( "Beenden", "Exit", typeof(CustomCommands), new InputGestureCollection() { new KeyGesture(Key.F4, ModifierKeys.Alt) } ); } public partial class MainWindow : Window { public CustomCommands CM; public MainWindow() { CM = new CustomCommands(); this.DataContext = CM; InitializeComponent(); } private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if(e != null) e.CanExecute = true; } private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e) { Application.Current.Shutdown(); } } 
0
source

Source: https://habr.com/ru/post/1241591/


All Articles