Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value cannot be null CommandBinding custom command

Tags:

c#

wpf

I've just started with WPF a few days ago and have come to a problem I dont understand.

I got the following error:

Value cannot be null. Parametername: value

The error occurs here:

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

I've of course set namespace xmlns:self="clr-namespace:PrintMonitor" in the xaml.

The code-behind:

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 does this error occurs if I use a custom command but not if i use e.g. Command="ApplicationCommands.New" and how can I fix this error?

The Code is part of this tutorial.

like image 221
Nitro.de Avatar asked Jan 27 '16 10:01

Nitro.de


1 Answers

Click on the button "Enable Project Code" Just under the designer window

Enable Project Code button

like image 87
Lenor Avatar answered Sep 21 '22 05:09

Lenor