Xaml 中的事件触发器

发布于 2024-11-30 22:58:37 字数 3417 浏览 1 评论 0原文

我试图在我的风格中添加一个事件触发器,但由于某种原因它不起作用。 它向我抛出一个错误 {“值不能为空。\r\n参数名称: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);
            }
        }
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2024-12-07 22:58:37

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文