Okay, so along with all the types of RoutedCommands, I have posted several extension methods to register a routed command with a type that is itself
public static class Commands { public static RoutedCommand ClearInputCommand = new RoutedCommand("ClearInputCommand", typeof(Commands)); public static void RegisterCommand(this UIElement uiElement, RoutedCommand command, ExecutedRoutedEventHandler execute, CanExecuteRoutedEventHandler canExecute = null) { uiElement.RegisterCommand(new CommandBinding(command, execute, canExecute)); } public static void RegisterCommand(this UIElement uiElement, CommandBinding commandBinding) { CommandManager.RegisterClassCommandBinding(typeof(object), commandBinding); } public static void UnRegisterCommand(this UIElement uiElement, RoutedCommand command) { for (int i = 0; i < uiElement.CommandBindings.Count; i++) { CommandBinding c = uiElement.CommandBindings[i]; if (c.Command == command) { uiElement.CommandBindings.RemoveAt(i); } } } public static void UnRegisterCommand(this UIElement uiElement, CommandBinding commandBinding) { uiElement.CommandBindings.Remove(commandBinding); } }
and then just called it from the constructor of this class, I'm not sure that I need to unregister this hard one, it seems to me that this can cause a memory leak, since it contains references to the Execute and CanExecute delegates.
to unregister, I would have to keep track of all registered accounts and clear CommandBindings when the application was closed.
I think the best solution would be to use something like CompositeCommand prisms. but now it will be done.
source share