Multi Event Command Binder

18 Dec

Multi Event Command

Donate
Project Description:
This project extends and improves existing command binder.It has feature of binding multiple events of a single control to multiple commands.
Benefits:
  1. Ability to bind multiple event to multiple commands.
  2. In CommandArgs in command handler you can even get original event args this was not possible with existing command binders.
  3. In CommandArgs in command handler you can even get original source of the event this was not possible with existing command binders.
  4. In this you can control individual CanExecute methods.
Project Contents:
To use this multi event command feature you should update the files like shown below.
   XAML:
<Canvas mecb:CommandBehavior.Event="MouseDown,MouseEnter,MouseMove,MouseUp,MouseLeave" Background="LightBlue" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" mecb:CommandBehavior.Command="{Binding DockPanelCommandCollection}" mecb:CommandBehavior.CommandParameter="{Binding ElementName=txtCommandParameter,Path=Text}" > <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap">Test Mouse Event In This Block</TextBlock> </Canvas> 
 C#
#region Constructor        public ClientUserControlViewModel()
        {
            DockPanelCommandCollection=new ObservableCollection<RelayCommand>
                                           {
                                               MouseDownCommand,
                                               MouseUpCommand,
                                               MouseEnterCommand,
                                               MouseMoveCommand,
                                               MouseLeaveCommand
                                           };
            MouseMoveIsChecked = true;
        }

        #endregion

        #region Members

        private RelayCommand _mouseDownCommand;
        private RelayCommand _mouseUpCommand;
        private RelayCommand _mouseMoveCommand;
        private RelayCommand _mouseEnterCommand;
        private RelayCommand _mouseLeaveCommand;
        private string _lastEvent;
        private string _leftButtonState;
        private string _rightButtonState;
        private string _commandParamterValue;

        #endregion

        #region Commands

        public RelayCommand MouseDownCommand
        {
           get { return _mouseDownCommand ?? (_mouseDownCommand = new RelayCommand(MouseDownCommandHandler,CanExecuteMouseDown){EventName = "MouseDown"})

               ; }

        }

        private bool CanExecuteMouseDown(object obj)
        {
            return !MouseDownIsChecked;
        }

        public RelayCommand MouseUpCommand
        {
            get { return _mouseUpCommand ?? (_mouseUpCommand = new RelayCommand(MouseUpCommandHandler, CanExecuteMouseUp) { EventName = "MouseUp" }); }

        }

        private bool CanExecuteMouseUp(object obj)
        {
            return !MouseUpIsChecked;
        }

        public RelayCommand MouseEnterCommand
        {
            get { return _mouseEnterCommand ?? (_mouseEnterCommand = new RelayCommand(MouseEnterCommandHandler,CanExecuteMouseEnter) { EventName = "MouseEnter" }); }

        }

        private bool CanExecuteMouseEnter(object obj)
        {
            return !MouseEnterIsChecked;
        }

        public RelayCommand MouseMoveCommand
        {
            get
            {
                return _mouseMoveCommand ??
                       (_mouseMoveCommand = new RelayCommand(MouseMoveCommandHandler,CanExecuteMouseMove) {EventName = "MouseMove"});
            }
        }

        private bool CanExecuteMouseMove(object obj)
        {
            return !MouseMoveIsChecked;
        }

        public RelayCommand MouseLeaveCommand
        {
            get { return _mouseLeaveCommand ?? (_mouseLeaveCommand = new RelayCommand(MouseLeaveCommandHandler,CanExecuteMouseLeave) { EventName = "MouseLeave" }); }

        }

        private bool CanExecuteMouseLeave(object obj)
        {
            return !MouseLeaveIsChecked;
        }

        #endregion

        #region Handlers

        private void MouseDownCommandHandler(CommandArgs obj)
        {
            LastEvent = "MouseDown";
            TryCatch.BeginTryCatch(UpdateResults,obj);
        }

        private void MouseUpCommandHandler(CommandArgs obj)
        {
            LastEvent = "MouseUp";
            TryCatch.BeginTryCatch(UpdateResults, obj);
        }

        private void MouseEnterCommandHandler(CommandArgs obj)
        {
            LastEvent = "MouseEnter";
            TryCatch.BeginTryCatch(UpdateResults, obj);
        }

        private void MouseMoveCommandHandler(CommandArgs obj)
        {
            LastEvent = "MouseMove";
            TryCatch.BeginTryCatch(UpdateResults, obj);
        }

        private void MouseLeaveCommandHandler(CommandArgs obj)
        {
            LastEvent = "MouseLeave";
            TryCatch.BeginTryCatch(UpdateResults, obj);
        }

        #endregion

        #region CommandCollection

        public ObservableCollection<RelayCommand> DockPanelCommandCollection { get; set; }

        #endregion

        #region Methods

        private void UpdateResults(CommandArgs obj)
        {
            var args = obj.OriginalArgs as MouseEventArgs;
            if(args!=null)
            {
                LeftButtonState = args.LeftButton.ToString();
                RightButtonState = args.RightButton.ToString();

            }
            if (obj.CommandParameters != null) CommandParameterValue = obj.CommandParameters.ToString();
        }

        #endregion

        #region Properties

        private bool  _mouseDownIsChecked;

        public bool MouseDownIsChecked
        {
            get { return  _mouseDownIsChecked; }
            set
            {
                 _mouseDownIsChecked = value;
                NotifyPropertyChanged(this, o => o.MouseDownIsChecked);
            }
        }

        private bool _mouseUpIsChecked;

        public bool MouseUpIsChecked
        {
            get { return _mouseUpIsChecked; }
            set
            {
                _mouseUpIsChecked = value;
                NotifyPropertyChanged(this, o => o.MouseUpIsChecked);
            }
        }

        private bool _mouseEnterIsChecked;

        public bool MouseEnterIsChecked
        {
            get { return _mouseEnterIsChecked; }
            set
            {
                _mouseEnterIsChecked = value;
                NotifyPropertyChanged(this, o => o.MouseEnterIsChecked);
            }
        }

        private bool _mouseLeaveIsChecked;

        public bool MouseLeaveIsChecked
        {
            get { return _mouseLeaveIsChecked; }
            set
            {
                _mouseLeaveIsChecked = value;
                NotifyPropertyChanged(this, o => o.MouseLeaveIsChecked);
            }
        }

        private bool _mouseMoveIsChecked;

        public bool MouseMoveIsChecked
        {
            get { return _mouseMoveIsChecked; }
            set
            {
                _mouseMoveIsChecked = value;
                NotifyPropertyChanged(this, o => o.MouseMoveIsChecked);
            }
        }

        public string LastEvent
        {
            get { return _lastEvent; }
            set
            {
                _lastEvent = value;
                NotifyPropertyChanged(this, o => o.LastEvent);
            }
        }

        public string LeftButtonState
        {
            get { return _leftButtonState; }
            set
            {
                _leftButtonState = value;
                NotifyPropertyChanged(this, o => o.LeftButtonState);
            }
        }

        public string RightButtonState
        {
            get { return _rightButtonState; }
            set
            {
                _rightButtonState = value;
                NotifyPropertyChanged(this, o => o.RightButtonState);
            }
        }

        public string CommandParameterValue
        {
            get { return _commandParamterValue; }
            set
            {
                _commandParamterValue = value;
                NotifyPropertyChanged(this, o => o.CommandParameterValue);
            }
        }

        #endregion
 
Donate
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: