0

I'm trying to learn Command Binding but with my first code I have an error.I searched a lot for resolve my problem but I wasn't successful. This is my XML Code :

<Window
    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:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    x:Class="Office.Windows.winMain"
    xmlns:local = "clr-namespace:Office"
    Closing="Window_Closing"
    mc:Ignorable="d" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" WindowState="Maximized"
    Title="Main Window" Height="459.593" Width="604.36">

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

And this is my code behind

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

    public static class CustomCommands
    {
        public static readonly RoutedUICommand Exit = new RoutedUICommand
            (
                "Exit",
                "Exit",
                typeof(CustomCommands),
                new InputGestureCollection()
                {
                new KeyGesture(Key.End, ModifierKeys.Control)
                }
            );
    }
    private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
     {
            e.CanExecute = true;
     }

     private void ExitCommand_Executed(object sender,ExecutedRoutedEventArgs e)
     {
          Application.Current.Shutdown();
     }
   }
}

But in this code I have the error The name "CustomCommands" does not exist in the namespace "clr-namespace:Office". And also I have to say my MainWindow is in Windows Folder in my project but I tried both namespace clr-namespace:Office and clr-namespace:Office.Windows. What's wrong with my code?

James
  • 1
  • 3

1 Answers1

0

Your CustomCommands class is defined inside your WinMain class. I suggest moving it to the top level in the namespace, and then using the namespace Office.Windows, if that is the namespace in which you put the class.

If you wish to keep it as it is, use clr-namespace:Office.Windows and then reference winMain.CustomCommands.Exit, as that is, where the commands currently reside.

Christoph Herold
  • 1,799
  • 13
  • 18
  • Thanks a lot.But still I have problem.when my window lost focus the command dose not working.Is there any way to have access to my command in all of my project? – James Mar 11 '19 at 15:34
  • Are you looking for this: https://stackoverflow.com/questions/7883004/wpf-commandbindings-defined-in-one-window-not-available-in-another – Christoph Herold Mar 11 '19 at 15:40
  • Yes something like this but I don't want to write my command binding in all of my window.Is there any way to have access the command that is in Main Window??? – James Mar 11 '19 at 15:50
  • Read the answers to the other question, specifically this one https://stackoverflow.com/a/52023930/2451871 :-) – Christoph Herold Mar 11 '19 at 16:33