icecub icecub - 2 months ago 29
C# Question

XAML CommandBinding does not exist within local namespace?

I'm trying to add custom commands to my project in C#. To do this I've added a new class to my project:

using System.Windows.Input;

namespace DataBindingHuiswerk
{
public static class CustomCommands
{
public static readonly RoutedUICommand Add = new RoutedUICommand(
"Add",
"Add",
typeof(CustomCommands),
new InputGestureCollection()
{
new KeyGesture(Key.F1, ModifierKeys.Control)
}
);

public static readonly RoutedUICommand Change = new RoutedUICommand(
"Change",
"Change",
typeof(CustomCommands),
new InputGestureCollection()
{
new KeyGesture(Key.F2, ModifierKeys.Control)
}
);

public static readonly RoutedUICommand Delete = new RoutedUICommand(
"Delete",
"Delete",
typeof(CustomCommands),
new InputGestureCollection()
{
new KeyGesture(Key.F3, ModifierKeys.Control)
}
);
}
}


Next I'm trying to bind these in my XAML code:

<Window x:Class="DataBindingHuiswerk.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataBindingHuiswerk"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<CommandBinding Command="local:CustomCommands.Add" Executed="AddCommand_Executed" CanExecute="AddCommand_CanExecute" />


However, this returns me:


The name "CustomCommands" does not exist in the namespace "clr-namespace:DataBindingHuiswerk"


Now I'm probably wrong here, but to me
CustomCommands
clearly exists within the
DataBindingHuiswerk
namespace? Am I missing something?

Answer

A few issues in your code.

Xaml should be like this.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.CommandBindings>
        <CommandBinding Command="local:CustomCommands.Add" Executed="AddCommand_Executed" CanExecute="CommandBinding_OnCanExecute" />
    </Window.CommandBindings>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Command="local:CustomCommands.Add">Add</Button>
    </StackPanel>
</Window>

Code behind should be like this.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void AddCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        Debug.WriteLine("sdfgsdgdsgdf");
    }

    private void CommandBinding_OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
}