5 using System.Windows.Interactivity;
7 using SiliconStudio.Presentation.Core;
8 using SiliconStudio.Presentation.Extensions;
10 using System.Windows.Input;
11 using System.Windows.Data;
13 namespace SiliconStudio.Presentation.Behaviors
21 private string propertyName;
23 private DependencyProperty dependencyProperty;
33 public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
"CommandParameter", typeof(
object), typeof(
OnPropertyChangedCommandBehavior));
38 public static readonly DependencyProperty ExecuteOnlyOnSourceUpdateProperty = DependencyProperty.Register(
"ExecuteOnlyOnSourceUpdate", typeof(
bool), typeof(
OnPropertyChangedCommandBehavior));
44 public string PropertyName {
get {
return propertyName; } set {
if (AssociatedObject == null) propertyName = value; } }
49 public ICommand Command {
get {
return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
54 public object CommandParameter {
get {
return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } }
60 public bool ExecuteOnlyOnSourceUpdate {
get {
return (
bool)GetValue(ExecuteOnlyOnSourceUpdateProperty); } set { SetValue(ExecuteOnlyOnSourceUpdateProperty, value); } }
64 if (PropertyName == null)
65 throw new ArgumentException(
string.Format(
"The PropertyName property must be set on behavior '{0}'.", GetType().FullName));
67 dependencyProperty = AssociatedObject.GetDependencyProperties(
true).FirstOrDefault(dp => dp.Name == PropertyName);
68 if (dependencyProperty == null)
69 throw new ArgumentException(
string.Format(
"Unable to find property '{0}' on object of type '{1}'.", PropertyName, AssociatedObject.GetType().FullName));
71 propertyWatcher.Attach(AssociatedObject);
73 propertyWatcher.RegisterValueChangedHandler(dependencyProperty, OnPropertyChanged);
74 Binding.AddSourceUpdatedHandler(AssociatedObject, OnSourceUpdated);
79 propertyWatcher.Detach();
83 private void OnSourceUpdated(
object sender, DataTransferEventArgs e)
85 if (ExecuteOnlyOnSourceUpdate && e.Property == dependencyProperty)
91 private void OnPropertyChanged(
object sender,
EventArgs e)
93 if (!ExecuteOnlyOnSourceUpdate)
99 private void ExecuteCommand()
101 if (Command == null || !Command.CanExecute(CommandParameter))
104 Command.Execute(CommandParameter);
A Behavior that allow to execute a command when the value of a dependency property of its associated ...
override void OnDetaching()
override void OnAttached()