Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CommandBindingBehavior.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Windows;
4 using System.Windows.Input;
5 using System.Windows.Interactivity;
6 
8 
9 namespace SiliconStudio.Presentation.Behaviors
10 {
11  /// <summary>
12  /// This command will bind a <see cref="ICommandBase"/> to a <see cref="RoutedCommand"/>. It works just as a <see cref="CommandBinding"/> except that the bound
13  /// command is executed when the routed command is executed. The <b>CanExecute</b> handler also invoke the <b>CanExecute</b> method of the <see cref="ICommandBase"/>.
14  /// </summary>
15  public class CommandBindingBehavior : Behavior<FrameworkElement>
16  {
17  private CommandBinding commandBinding;
18 
19  /// <summary>
20  /// Identifies the <see cref="Command"/> dependency property.
21  /// </summary>
22  public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommandBase), typeof(CommandBindingBehavior), new PropertyMetadata(null, CommandChanged));
23 
24  /// <summary>
25  /// Identifies the <see cref="IsEnabled"/> dependency property.
26  /// </summary>
27  public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(CommandBindingBehavior), new PropertyMetadata(true));
28 
29  /// <summary>
30  /// Gets or sets the <see cref="RoutedCommand"/> to bind.
31  /// </summary>
32  public RoutedCommand RoutedCommand { get; set; }
33 
34  /// <summary>
35  /// Gets or sets the <see cref="ICommandBase"/> to bind.
36  /// </summary>
37  public ICommandBase Command { get { return (ICommandBase)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
38 
39  /// <summary>
40  /// Gets or sets whether this command binding is enabled. When disabled, the <see cref="Command"/> won't be executed.
41  /// </summary>
42  public bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { SetValue(IsEnabledProperty, value); } }
43 
44  /// <inheritdoc/>
45  protected override void OnAttached()
46  {
47  commandBinding = new CommandBinding(RoutedCommand, (s, e) => OnExecuted(e), (s, e) => OnCanExecute(e));
48  AssociatedObject.CommandBindings.Add(commandBinding);
49  }
50 
51  /// <inheritdoc/>
52  protected override void OnDetaching()
53  {
54  AssociatedObject.CommandBindings.Remove(commandBinding);
55  }
56 
57  private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
58  {
59  CommandManager.InvalidateRequerySuggested();
60  }
61 
62  private void OnCanExecute(CanExecuteRoutedEventArgs canExecuteRoutedEventArgs)
63  {
64  if (Command != null)
65  {
66  canExecuteRoutedEventArgs.CanExecute = IsEnabled && Command.CanExecute(canExecuteRoutedEventArgs.Parameter);
67  canExecuteRoutedEventArgs.Handled = true;
68  }
69  else
70  {
71  canExecuteRoutedEventArgs.CanExecute = true;
72  }
73  }
74 
75  private void OnExecuted(ExecutedRoutedEventArgs executedRoutedEventArgs)
76  {
77  if (Command != null && IsEnabled)
78  {
79  Command.Execute(executedRoutedEventArgs.Parameter);
80  executedRoutedEventArgs.Handled = true;
81  }
82  }
83  }
84 }
This command will bind a ICommandBase to a RoutedCommand. It works just as a CommandBinding except th...
An interface representing an implementation of ICommand with additional properties.
Definition: ICommandBase.cs:10
function s(a)