Example #1
0
        private void UpdateEnabledState()
        {
            if (_enabledPropertyInfo == null)
            {
                return;
            }

            var commandParameter = _commandParameterBinding.GetBindingValue();

            _enabledPropertyInfo.SetValue(_element, _command.CanExecute(commandParameter));
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandBinding"/> class.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="eventName">Name of the event.</param>
        /// <param name="command">The command.</param>
        /// <param name="commandParameterBinding">The command parameter binding.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="element"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="eventName"/> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="command"/> is <c>null</c>.</exception>
        public CommandBinding(object element, string eventName, ICatelCommand command, Binding commandParameterBinding = null)
        {
            Argument.IsNotNull("element", element);
            Argument.IsNotNullOrWhitespace("eventName", eventName);
            Argument.IsNotNull("command", command);

            _element = element;
            _command = command;
            _commandParameterBinding = commandParameterBinding;

            var elementType = _element.GetType();

            _eventInfo = elementType.GetEventEx(eventName);
            if (_eventInfo == null)
            {
                Log.ErrorAndThrowException <InvalidOperationException>("Event '{0}.{1}' not found, cannot create command binding", elementType.Name, eventName);
            }

            _enabledPropertyInfo = elementType.GetPropertyEx("Enabled");

            _eventHandler = delegate
            {
                var commandParameter = _commandParameterBinding.GetBindingValue();
                if (_command.CanExecute(commandParameter))
                {
                    _command.Execute(commandParameter);
                }
            };
            _eventInfo.AddEventHandler(element, _eventHandler);

            _canExecuteChangedHandler  = (sender, e) => UpdateEnabledState();
            command.CanExecuteChanged += _canExecuteChangedHandler;

            if (commandParameterBinding != null)
            {
                _commandBindingParameterValueChangedHandler = (sender, e) => UpdateEnabledState();
                commandParameterBinding.ValueChanged       += _commandBindingParameterValueChangedHandler;
            }

            UpdateEnabledState();
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandBinding"/> class.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="eventName">Name of the event.</param>
        /// <param name="command">The command.</param>
        /// <param name="commandParameterBinding">The command parameter binding.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="element"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="eventName"/> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="command"/> is <c>null</c>.</exception>
        public CommandBinding(object element, string eventName, ICatelCommand command, Binding commandParameterBinding = null)
        {
            Argument.IsNotNull("element", element);
            Argument.IsNotNullOrWhitespace("eventName", eventName);
            Argument.IsNotNull("command", command);

            _element = element;
            _command = command;
            _commandParameterBinding = commandParameterBinding;

            var elementType = _element.GetType();
            _eventInfo = elementType.GetEventEx(eventName);
            if (_eventInfo == null)
            {
                throw Log.ErrorAndCreateException<InvalidOperationException>("Event '{0}.{1}' not found, cannot create command binding", elementType.Name, eventName);
            }

            _enabledPropertyInfo = elementType.GetPropertyEx("Enabled");

            _eventHandler = delegate
            {
                var commandParameter = _commandParameterBinding.GetBindingValue();
                if (_command.CanExecute(commandParameter))
                {
                    _command.Execute(commandParameter);
                }
            };
            _eventInfo.AddEventHandler(element, _eventHandler);

            _canExecuteChangedHandler = (sender, e) => UpdateEnabledState();
            command.CanExecuteChanged += _canExecuteChangedHandler;

            if (commandParameterBinding != null)
            {
                _commandBindingParameterValueChangedHandler = (sender, e) => UpdateEnabledState();
                commandParameterBinding.ValueChanged += _commandBindingParameterValueChangedHandler;
            }

            UpdateEnabledState();
        }