/// <summary> /// Callback when the Command property is set or changed. /// </summary> private static void OnCommandInvalidated(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { if (dependencyObject == null) { throw new ArgumentNullException("dependencyObject"); } UIElement element = (UIElement)dependencyObject; // Clear the exisiting bindings on the element we are attached to. element.CommandBindings.Clear(); // If we're given a new command model, set up a binding. if (e.NewValue is CommandModel) { CommandModel commandModel = e.NewValue as CommandModel; element.CommandBindings.Add(new CommandBinding(commandModel.Command, commandModel.OnExecute, commandModel.OnQueryEnabled)); } // Suggest to WPF to refresh commands. CommandManager.InvalidateRequerySuggested(); }
/// <summary> /// Method used to set a single binding on an element. /// Any exsisting bindings will be removed. /// </summary> public static void SetCommand(DependencyObject sender, CommandModel command) { sender.SetValue(CommandProperty, command); }