private static void OnCommandChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { Control control = dependencyObject as Control; if (control != null) { ControlEventCommandBehavior behavior = GetOrCreateBehavior(control); behavior.Command = (e.NewValue as ICommand); } }
//Factory private static ControlEventCommandBehavior GetOrCreateBehavior(Control control) { string eventName = GetEventName(control); if (string.IsNullOrEmpty(eventName) || !Behaviors.ContainsKey(eventName)) { eventName = ClickEventName; } ControlEventCommandBehavior behavior = (ControlEventCommandBehavior)control.GetValue(EventCommandBehaviorProperty); if (behavior == null) { Type behaviorType = Behaviors[eventName]; behavior = (ControlEventCommandBehavior)Activator.CreateInstance(behaviorType, control); control.SetValue(EventCommandBehaviorProperty, behavior); } return(behavior); }