WPF Commands, How to declare Application level commands?
You can setup CommandBindings for "All Windows" of your WPF application and implement command handlers in Application class.
First of all, create a static command container class. For example,
namespace WpfApplication1
{
public static class MyCommands
{
private static readonly RoutedUICommand doSomethingCommand = new RoutedUICommand("description", "DoSomethingCommand", typeof(MyCommands));
public static RoutedUICommand DoSomethingCommand
{
get
{
return doSomethingCommand;
}
}
}
}
Next, set your custom command to Button.Command like this.
<Window x:Class="WpfApplication1.MainWindow"
...
xmlns:local="clr-namespace:WpfApplication1">
<Grid>
...
<Button Command="local:MyCommands.DoSomethingCommand">Execute</Button>
</Grid>
</Window>
Finally, implement the command handler of your custom command in Application class.
namespace WpfApplication1
{
public partial class App : Application
{
public App()
{
var binding = new CommandBinding(MyCommands.DoSomethingCommand, DoSomething, CanDoSomething);
// Register CommandBinding for all windows.
CommandManager.RegisterClassCommandBinding(typeof(Window), binding);
}
private void DoSomething(object sender, ExecutedRoutedEventArgs e)
{
...
}
private void CanDoSomething(object sender, CanExecuteRoutedEventArgs e)
{
...
e.CanExecute = true;
}
}
}
I did not like the complexity of the other solutions, but after a few hours of research I found out it is really simple.
First setup your command as you usually do, but add a static property for WPF so that it can obtain an instance of your command.
class MyCommand : ICommand
{
// Singleton for the simple cases, may be replaced with your own factory
public static ICommand Instance { get; } = new MyCommand();
public bool CanExecute(object parameter)
{
return true; // TODO: Implement
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
// TODO: Implement
}
}
Add a reference to the namespace of your command in your XAML (last line), like this:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:commands="clr-namespace:MyProject.Commands">
Then just reference your static property in your XAML like this:
<Button Content="Button" Command="commands:MyCommand.Instance" />