using System.Windows; using System.Windows.Input; namespace OpenSEMI.ClientBase.Command { public abstract class CommandTrigger : Freezable, ICommandTrigger { public bool IsInitialized { get; set; } #region Dependency Properties #region Command Property /// Identifies the Command dependency property public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTrigger), new FrameworkPropertyMetadata(null)); /// description for Command property public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } #endregion #region CustomParameter Property /// Identifies the CustomParameterProperty dependency property public static readonly DependencyProperty CustomParameterProperty = DependencyProperty.Register("CustomParameter", typeof(object), typeof(CommandTrigger), new FrameworkPropertyMetadata(null)); /// description for CustomParameter property public object CustomParameter { get { return (object)GetValue(CustomParameterProperty); } set { SetValue(CustomParameterProperty, value); } } #endregion #region CommandTarget Property /// Identifies the CommandTarget dependency property public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTrigger), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits)); /// description for CommandTarget property public IInputElement CommandTarget { get { return (IInputElement)GetValue(CommandTargetProperty); } set { SetValue(CommandTargetProperty, value); } } #endregion #endregion #region Internals void ICommandTrigger.Initialize(FrameworkElement source) { if (IsInitialized) return; InitializeCore(source); IsInitialized = true; } protected abstract void InitializeCore(FrameworkElement source); protected void ExecuteCommand(CommandParameter parameter) { if (Command == null) return; RoutedCommand routedCommand = Command as RoutedCommand; if (routedCommand != null) { routedCommand.Execute(parameter, CommandTarget); } else { Command.Execute(parameter); } } #endregion } }