Exemplo n.º 1
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();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Removes the binding.
        /// </summary>
        /// <param name="binding">The binding.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="binding"/> is <c>null</c>.</exception>
        public void RemoveBinding(Binding binding)
        {
            Argument.IsNotNull("binding", binding);

            Log.Debug("Removing binding '{0}'", binding);

            for (int i = 0; i < _bindings.Count; i++)
            {
                if (ReferenceEquals(_bindings[i], binding))
                {
                    _bindings.RemoveAt(i);
                    return;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds a new binding.
        /// </summary>
        /// <param name="binding">The binding.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="binding"/> is <c>null</c>.</exception>
        public void AddBinding(Binding binding)
        {
            Argument.IsNotNull("binding", binding);

            Log.Debug("Adding binding '{0}'", binding);

            _bindings.Add(binding);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Uninitializes this binding.
        /// </summary>
        protected override void Uninitialize()
        {
            if (_eventHandler != null)
            {
                _eventInfo.RemoveEventHandler(_element, _eventHandler);
                _eventHandler = null;
            }

            if (_canExecuteChangedHandler != null)
            {
                _command.CanExecuteChanged -= _canExecuteChangedHandler;
                _canExecuteChangedHandler = null;
            }

            if (_commandBindingParameterValueChangedHandler != null)
            {
                _commandParameterBinding.ValueChanged -= _commandBindingParameterValueChangedHandler;
                _commandBindingParameterValueChangedHandler = null;
            }

            _element = null;
            _eventInfo = null;
            _enabledPropertyInfo = null;
            _command = null;
            _commandParameterBinding = null;

            // TODO: call commandParameterBinding.ClearBinding();?
        }