Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class CommandBank
- {
- /// Command definition for Closing a window
- public static RoutedUICommand CloseWindow { get; private set; }
- /// Static private constructor, sets up all application wide commands.
- static CommandBank()
- {
- CloseWindow = new RoutedUICommand();
- CloseWindow.InputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.Alt));
- // ...
- }
- /// Closes the window provided as a parameter
- public static void CloseWindowExecute(object sender, ExecutedRoutedEventArgs e)
- {
- ((Window)e.Parameter).Close();
- }
- /// Allows a Command to execute if the CommandParameter is not a null value
- public static void CanExecuteIfParameterIsNotNull(object sender, CanExecuteRoutedEventArgs e)
- {
- e.CanExecute = e.Parameter != null;
- e.Handled = true;
- }
- public partial class SimpleWindow : Window
- {
- private void WindowLoaded(object sender, RoutedEventArgs e)
- {
- // ...
- this.CommandBindings.Add(
- new CommandBinding(
- CommandBank.CloseWindow,
- CommandBank.CloseWindowExecute,
- CommandBank.CanExecuteIfParameterIsNotNull));
- foreach (CommandBinding binding in this.CommandBindings)
- {
- RoutedCommand command = (RoutedCommand)binding.Command;
- if (command.InputGestures.Count > 0)
- {
- foreach (InputGesture gesture in command.InputGestures)
- {
- var iBind = new InputBinding(command, gesture);
- iBind.CommandParameter = this;
- this.InputBindings.Add(iBind);
- }
- }
- }
- // menuItemExit is defined in XAML
- menuItemExit.Command = CommandBank.CloseWindow;
- menuItemExit.CommandParameter = this;
- // ...
- }
- // ....
- }
Add Comment
Please, Sign In to add comment