maridob
maridob

Reputation: 661

EventTrigger in Xaml

I am trying to add an eventtrigger inside my style but for some reason it is not working. It is throwing me an error of {"Value cannot be null.\r\nParameter name: routedEvent"} My goal is to add a fix logic of my control in its KeyDown event, anybody has any idea of what's going on?

The below style is inside my Theme.xaml.

    <Style.Triggers>
            <EventTrigger>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="KeyDown">
                    <Behaviors:ExecuteCommandAction Command="{Binding TabKeyDownCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </EventTrigger>
    </Style.Triggers>

Here is my ExecuteCommandAction class:

/// <summary>
/// Behaviour helps to bind any RoutedEvent of UIElement to Command.
/// </summary>
[DefaultTrigger(typeof (UIElement), typeof (EventTrigger), "KeyDown")]
public class ExecuteCommandAction : TargetedTriggerAction<UIElement>
{
    /// <summary>
    /// Dependency property represents the Command of the behaviour.
    /// </summary>
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter",
                                            typeof (object),
                                            typeof (ExecuteCommandAction),
                                            new FrameworkPropertyMetadata(null));

    /// <summary>
    /// Dependency property represents the Command parameter of the behaviour.
    /// </summary>
    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command",
                                                                                                    typeof (ICommand
                                                                                                        ),
                                                                                                    typeof (
                                                                                                        ExecuteCommandAction
                                                                                                        ),
                                                                                                    new FrameworkPropertyMetadata
                                                                                                        (null));

    /// <summary>
    /// Gets or sets the Commmand.
    /// </summary>
    public ICommand Command
    {
        get { return (ICommand) GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    /// <summary>
    /// Gets or sets the CommandParameter.
    /// </summary>
    public object CommandParameter
    {
        get { return GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }


    /// <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 (Command != null)
        {
            if (Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter);
            }
        }
    }
}

Upvotes: 0

Views: 1524

Answers (1)

brunnerh
brunnerh

Reputation: 185445

Interactivity functionality cannot be used like this, it does not belong to normal Triggers, EventTriggers or even styles themselves. You can use it on elements like this:

<Button>
    <i:Interaction.Triggers>
         <!-- ... -->
    </i:Interaction.Triggers>
</Button>

The error is caused by not setting RoutedEvent on the encompassing EventTrigger, but even if you did set it, new errors would follow.

Upvotes: 2

Related Questions