I am about to learn CommandBindings in WPF, but somehow I cannot create custom commands.
Following error is shown: (XDG0008) The name "CustomCommands" does not exist in the namespace "clr-namespace:CommandBindingWPF"
<Window.CommandBindings>
<CommandBinding Command="local:CustomCommands.Exit" CanExecute="ExitCommand_CanExecute" Executed="ExitCommand_Executed" />
</Window.CommandBindings>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = viewmodel;
}
//more code but not relevant for question
private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
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
(
"Exit",
"Exit",
typeof(CustomCommands)
);
//Define more commands here, just like the one above
}
I even tried to copy-paste it from https://wpf-tutorial.com/commands/implementing-custom-commands/ and paste the CanExecute
and Executed
just for testing in MainWindow
-class (and of course i changed the xmlns:self
), but it still does not work. Can someone help me out? It is making me crazy.