forked from SonyWWS/ATF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandReference.cs
113 lines (95 loc) · 3.89 KB
/
CommandReference.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//Copyright © 2014 Sony Computer Entertainment America LLC. See License.txt.
using System;
using System.Windows;
using System.Windows.Input;
namespace Sce.Atf.Wpf
{
/// <summary>
/// This class facilitates associating a key binding in XAML markup to a command
/// defined in a View Model by exposing a Command dependency property.
/// The class derives from Freezable to work around a limitation in WPF when data-binding from XAML.</summary>
public class CommandReference : Freezable, ICommand, IDisposable
{
/// <summary>
/// Constructor</summary>
public CommandReference()
{
}
/// <summary>
/// Dependency property representing a command</summary>
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandReference), new PropertyMetadata(OnCommandChanged));
/// <summary>
/// Gets or sets the effective value of the Command DependencyProperty</summary>
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
#region ICommand Members
/// <summary>
/// Indicates whether the command can execute in its current state</summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed,
/// this object can be set to null</param>
/// <returns>True iff this command can be executed</returns>
public bool CanExecute(object parameter)
{
return Command != null && Command.CanExecute(parameter);
}
/// <summary>
/// Executes the command</summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed,
/// this object can be set to null</param>
public void Execute(object parameter)
{
Command.Execute(parameter);
}
/// <summary>
/// CanExecuteChanged event</summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Raises the CanExecuteChanged event</summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event arguments</param>
private void RaiseCanExecuteChanged(object sender, EventArgs e)
{
//CanExecuteChanged.Raise(sender, e);
}
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var commandReference = d as CommandReference;
var oldCommand = e.OldValue as ICommand;
var newCommand = e.NewValue as ICommand;
if (oldCommand != null)
{
oldCommand.CanExecuteChanged -= (s, args) => commandReference.RaiseCanExecuteChanged(s, args);
}
if (newCommand != null)
{
newCommand.CanExecuteChanged += (s, args) => commandReference.RaiseCanExecuteChanged(s, args);
}
}
#endregion
#region Freezable
/// <summary>
/// Creates a new instance of the System.Windows.Freezable derived class</summary>
/// <returns>New instance of the System.Windows.Freezable derived class</returns>
protected override Freezable CreateInstanceCore()
{
throw new NotSupportedException();
}
#endregion
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources</summary>
public void Dispose()
{
//test code
}
#endregion
}
}