0

我正在尝试在我的样式中添加一个事件触发器,但由于某种原因它不起作用。它给我一个错误 {"Value cannot be null.\r\nParameter name: routedEvent"} 我的目标是在其 KeyDown 事件中添加我的控件的修复逻辑,有人知道发生了什么吗?

下面的样式在我的 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>

这是我的 ExecuteCommandAction 类:

/// <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);
            }
        }
    }
}
4

1 回答 1

2

Interactivity功能不能这样使用,它不属于 normal TriggersEventTriggers甚至不属于样式本身。您可以在这样的元素上使用它:

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

该错误是由于未设置RoutedEvent包含而引起的EventTrigger,但即使您确实设置了它,也会出现新的错误。

于 2011-08-23T20:13:44.400 回答