Ejemplo n.º 1
0
        /// <summary>
        /// Executes the trigger.
        /// <para>To access the EventArgs of the fired event, use a RelayCommand&lt;EventArgs&gt;
        /// and leave the CommandParameter and CommandParameterValue empty!</para>
        /// </summary>
        /// <param name="parameter">The EventArgs of the fired event.</param>
        protected override void Invoke(object parameter)
        {
            if (AssociatedElementIsDisabled() &&
                !AlwaysInvokeCommand)
            {
                return;
            }

            var command          = GetCommand();
            var commandParameter = CommandParameterValue;

            if (commandParameter == null &&
                PassEventArgsToCommand)
            {
                commandParameter = EventArgsConverter == null
                    ? parameter
                    : EventArgsConverter.Convert(parameter, EventArgsConverterParameter);
            }

            if (command != null &&
                command.CanExecute(commandParameter))
            {
                command.Execute(commandParameter);
            }
        }
Ejemplo n.º 2
0
        protected override void Invoke(object parameter)
        {
            var cmd = Command;

            if (cmd != null)
            {
                var param = parameter;

                if (EventArgsConverter != null)
                {
                    param = EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.InvariantCulture);
                }
                else if (parameter.GetType() is { } type&& type.IsGenericType && type.GetGenericTypeDefinition() is { } gType)
                {
                    if (gType == typeof(RoutedEventArgs <>))
                    {
                        param = type.GetProperty(nameof(RoutedEventArgs <object> .Value)).GetValue(parameter);
                    }
                    else if (gType == typeof(RoutedPropertyChangedEventArgs <>))
                    {
                        param = type.GetProperty(nameof(RoutedPropertyChangedEventArgs <object> .NewValue)).GetValue(parameter);
                    }
                }
                if (cmd.CanExecute(param))
                {
                    cmd.Execute(param);
                }
            }
        }
        private void OnFired(object sender, EventArgs eventArgs)
        {
            if (Command == null)
            {
                return;
            }

            var parameter = CommandParameter;

            if (eventArgs != null && eventArgs != EventArgs.Empty)
            {
                parameter = eventArgs;

                if (EventArgsConverter != null)
                {
                    parameter = EventArgsConverter.Convert(eventArgs, typeof(object), EventArgsConverterParameter, CultureInfo.CurrentUICulture);
                }
            }

            //执行命令,并带有参数parameter。
            if (Command.CanExecute(parameter))
            {
                Command.Execute(parameter);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes the command
        /// </summary>
        /// <param name="parameter">This parameter is passed to the command; the CommandParameter specified in the CommandParameterProperty is used for command invocation if not null.</param>
        protected override void Invoke(object parameter)
        {
            if (!string.IsNullOrEmpty(TriggerParameterPath))
            {
                //Walk the ParameterPath for nested properties.
                var    propertyPathParts = TriggerParameterPath.Split('.');
                object propertyValue     = parameter;
                foreach (var propertyPathPart in propertyPathParts)
                {
                    var propInfo = propertyValue.GetType().GetTypeInfo().GetProperty(propertyPathPart);
                    propertyValue = propInfo.GetValue(propertyValue);
                }
                parameter = propertyValue;
            }

            if (EventArgsConverter != null)
            {
                parameter = EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.CurrentUICulture);
            }

            var behavior = GetOrCreateBehavior();

            if (behavior != null)
            {
                behavior.ExecuteCommand(parameter);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Invokes the action.
        /// </summary>
        /// <param name="parameter">The parameter to the action. If the action does not require a parameter, the parameter may be set to a null reference.</param>
        protected override void Invoke(object parameter)
        {
            if (AssociatedObject != null)
            {
                ICommand command = ResolveCommand();

                if (command != null)
                {
                    object commandParameter = CommandParameter;

                    //if no CommandParameter has been provided, let's check the EventArgsParameterPath
                    if (commandParameter == null && !string.IsNullOrWhiteSpace(EventArgsParameterPath))
                    {
                        commandParameter = GetEventArgsPropertyPathValue(parameter);
                    }

                    //next let's see if an event args converter has been supplied
                    if (commandParameter == null && EventArgsConverter != null)
                    {
                        commandParameter = EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.CurrentCulture);
                    }

                    //last resort, let see if they want to force the event args to be passed as a parameter
                    if (commandParameter == null && PassEventArgsToCommand)
                    {
                        commandParameter = parameter;
                    }

                    if (command.CanExecute(commandParameter))
                    {
                        command.Execute(commandParameter);
                    }
                }
            }
        }
        protected override void Invoke(object sender, object eventArgs)
        {
            object commandParameter = CommandParameter;
            bool   useConverter     = CommandParameter == null && ActualPassEventArgsToCommand;

            if (useConverter)
            {
                if (EventArgsConverter != null)
                {
                    commandParameter = EventArgsConverter.Convert(sender, eventArgs);
                }
                else
                {
                    commandParameter = eventArgs;
                }
            }
            if (CommandCanExecute(commandParameter))
            {
                CommandExecute(commandParameter);
            }
            if (useConverter && EventArgsConverter is IEventArgsTwoWayConverter)
            {
                ((IEventArgsTwoWayConverter)EventArgsConverter).ConvertBack(sender, eventArgs, commandParameter);
            }
        }
        private void OnFired(object sender, EventArgs eventArgs)
        {
            CommandEventData commandEventData;
            object           eventArgsparameter;

            if (Command == null)
            {
                return;
            }

            eventArgsparameter = eventArgs;

            if (eventArgs != null && eventArgs != EventArgs.Empty)
            {
                if (EventArgsConverter != null)
                {
                    eventArgsparameter = EventArgsConverter.Convert(eventArgs, typeof(object), EventArgsConverterParameter, CultureInfo.CurrentUICulture);
                }
            }

            commandEventData = new CommandEventData
            {
                Parameter = CommandParameter,
                Args      = eventArgsparameter,
                Sender    = sender
            };

            if (Command.CanExecute(commandEventData))
            {
                Command.Execute(commandEventData);
            }
        }
        private void OnEventRaised(object sender, EventArgs e)
        {
            if (Command != null)
            {
                object parameter;

                if (EventArgsConverter != null)
                {
                    parameter = EventArgsConverter.Convert(e, typeof(object), EventArgsConverterParameter, CultureInfo.CurrentUICulture);
                }
                else
                {
                    parameter = CommandParameter;
                }

                if (Command.CanExecute(parameter))
                {
                    Command.Execute(parameter);
                }
            }
#if DEBUG
            else
            {
                Debug.WriteLine($"{nameof(EventToCommandBehavior)}: missing Command on event handler, {EventName}: Sender={sender}, EventArgs={e}");
            }
#endif
        }
Ejemplo n.º 9
0
        protected virtual void OnEventRaised(object sender, EventArgs eventArgs)
        {
            if (Command == null)
            {
                return;
            }

            var parameter = CommandParameter;

            if (parameter == null && !string.IsNullOrEmpty(EventArgsParameterPath))
            {
                //Walk the ParameterPath for nested properties.
                var    propertyPathParts = EventArgsParameterPath.Split('.');
                object propertyValue     = eventArgs;
                foreach (var propertyPathPart in propertyPathParts)
                {
                    var propInfo = propertyValue.GetType().GetTypeInfo().GetDeclaredProperty(propertyPathPart);
                    propertyValue = propInfo.GetValue(propertyValue);
                }
                parameter = propertyValue;
            }

            if (parameter == null && eventArgs != null && eventArgs != EventArgs.Empty && EventArgsConverter != null)
            {
                parameter = EventArgsConverter.Convert(eventArgs, typeof(object), EventArgsConverterParameter,
                                                       CultureInfo.CurrentUICulture);
            }

            if (Command.CanExecute(parameter))
            {
                Command.Execute(parameter);
            }
        }
Ejemplo n.º 10
0
        protected virtual void OnEventRaised(object sender, EventArgs eventArgs)
        {
            if (Command == null)
            {
                return;
            }

            var parameter = CommandParameter;

            if (parameter == null && !string.IsNullOrEmpty(EventArgsParameterPath))
            {
                if (EventArgsParameterPath == ".")
                {
                    parameter = eventArgs;
                }
                else
                {
                    //Walk the ParameterPath for nested properties.
                    var    propertyPathParts = EventArgsParameterPath.Split('.');
                    object propertyValue     = eventArgs;
                    foreach (var propertyPathPart in propertyPathParts)
                    {
                        var propInfo = propertyValue.GetType().GetRuntimeProperty(propertyPathPart);
                        if (propInfo == null)
                        {
                            throw new MissingMemberException($"Unable to find {EventArgsParameterPath}");
                        }

                        propertyValue = propInfo.GetValue(propertyValue);
                        if (propertyValue == null)
                        {
                            break;
                        }
                    }
                    parameter = propertyValue;
                }
            }

            if (parameter == null && eventArgs != null && eventArgs != EventArgs.Empty && EventArgsConverter != null)
            {
                parameter = EventArgsConverter.Convert(eventArgs, typeof(object), EventArgsConverterParameter,
                                                       CultureInfo.CurrentUICulture);
            }

            if (Command.CanExecute(parameter))
            {
                Command.Execute(parameter);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Invokes the action.
        /// </summary>
        /// <param name="parameter">The parameter to the action. If the Action does not require a parameter, the parameter may be set to a null reference.</param>
        protected override void Invoke(object parameter)
        {
            if (PreventInvocationIfAssociatedObjectIsDisabled && IsAssociatedObjectDisabled())
            {
                return;
            }

            var commandParameter = CommandParameter;

            if ((commandParameter is null) && PassEventArgsToCommand)
            {
                commandParameter = EventArgsConverter != null?EventArgsConverter.Convert(AssociatedObject, parameter) : parameter;
            }

            ExecuteCommand(commandParameter);
        }
Ejemplo n.º 12
0
        protected override void Invoke(object parameter)
        {
            if (Command != null)
            {
                var param = parameter;

                if (EventArgsConverter != null)
                {
                    param = EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.InvariantCulture);
                }

                if (Command.CanExecute(param))
                {
                    Command.Execute(param);
                }
            }
        }
Ejemplo n.º 13
0
        protected override void Invoke(object parameter)
        {
            var command          = GetCommand();
            var commandParameter = CommandParameterValue;

            if (commandParameter == null)
            {
                commandParameter = EventArgsConverter == null
                    ? parameter
                    : EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.CurrentUICulture);
            }

            if (command != null && command.CanExecute(commandParameter))
            {
                command.Execute(commandParameter);
            }
        }
Ejemplo n.º 14
0
        protected override void OnTriggerHandled(object?sender = null, object?eventArgs = null)
        {
            var parameter = CommandParameter
                            ?? EventArgsConverter?.Convert(eventArgs, typeof(object), null, null)
                            ?? eventArgs;

            if (parameter is not TType)
            {
                // changing it to the default value to avoid a cast exception
                parameter = default(TType);
            }

            var command = Command;

            if (command?.CanExecute(parameter) ?? false)
            {
                command.Execute(parameter);
            }
        }
Ejemplo n.º 15
0
        protected override void Invoke(object sender, object eventArgs)
        {
            object commandParameter = CommandParameter;

            if (commandParameter == null && ActualPassEventArgsToCommand)
            {
                if (EventArgsConverter != null)
                {
                    commandParameter = EventArgsConverter.Convert(sender, eventArgs);
                }
                else
                {
                    commandParameter = eventArgs;
                }
            }
            if (CommandCanExecute(commandParameter))
            {
                CommandExecute(commandParameter);
            }
        }
Ejemplo n.º 16
0
        protected override void OnEvent(object sender, object eventArgs)
        {
            if (eventArgs is MouseButtonEventArgs e)
            {
                if (e.ChangedButton == MouseButton && (ModifierKeys == null || Keyboard.Modifiers.HasFlag(ModifierKeys)))
                {
                    e.Handled = MarkRoutedEventsAsHandled;

                    if (PassEventArgsToCommand == true && EventArgsConverter != null)
                    {
                        CommandParameter = EventArgsConverter.Convert(sender, eventArgs);
                    }

                    if (ExecuteCommandWhenParameterNull || CommandParameter != null)
                    {
                        Command?.Execute(CommandParameter);
                    }
                }
            }
        }
Ejemplo n.º 17
0
        void Invoke(object parameter)
        {
            if (!CanInvokeCommand())
            {
                return;
            }
            object commandParameter = CommandParameter;

            if (commandParameter == null && PassEventArgsToCommand)
            {
                if (EventArgsConverter != null)
                {
                    parameter = EventArgsConverter.Convert(parameter);
                }
                commandParameter = parameter;
            }
            if (Command != null && Command.CanExecute(commandParameter))
            {
                Command.Execute(commandParameter);
            }
        }