123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System.Windows;
- using System.Windows.Input;
- namespace OpenSEMI.ClientBase.Command
- {
- public abstract class CommandTrigger : Freezable, ICommandTrigger
- {
- public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTrigger), new FrameworkPropertyMetadata(null));
- public static readonly DependencyProperty CustomParameterProperty = DependencyProperty.Register("CustomParameter", typeof(object), typeof(CommandTrigger), new FrameworkPropertyMetadata(null));
- public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTrigger), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
- public bool IsInitialized
- {
- get;
- set;
- }
- public ICommand Command
- {
- get
- {
- return (ICommand)GetValue(CommandProperty);
- }
- set
- {
- SetValue(CommandProperty, value);
- }
- }
- public object CustomParameter
- {
- get
- {
- return GetValue(CustomParameterProperty);
- }
- set
- {
- SetValue(CustomParameterProperty, value);
- }
- }
- public IInputElement CommandTarget
- {
- get
- {
- return (IInputElement)GetValue(CommandTargetProperty);
- }
- set
- {
- SetValue(CommandTargetProperty, value);
- }
- }
- void ICommandTrigger.Initialize(FrameworkElement source)
- {
- if (!IsInitialized)
- {
- InitializeCore(source);
- IsInitialized = true;
- }
- }
- protected abstract void InitializeCore(FrameworkElement source);
- protected void ExecuteCommand(CommandParameter<object> parameter)
- {
- if (Command != null)
- {
- RoutedCommand routedCommand = Command as RoutedCommand;
- if (routedCommand != null)
- {
- routedCommand.Execute(parameter, CommandTarget);
- }
- else
- {
- Command.Execute(parameter);
- }
- }
- }
- }
- }
|