首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xaml中的EventTrigger

Xaml中的EventTrigger
EN

Stack Overflow用户
提问于 2011-08-24 04:07:56
回答 1查看 1.4K关注 0票数 0

我试图在我的风格中添加一个事件触发器,但由于某些原因它不起作用。它抛给我一个错误{“值不能为空。\r\n参数名: routedEvent"}我的目标是在它的KeyDown事件中添加我的控件的修复逻辑,有人知道这是怎么回事吗?

下面的样式在我的Theme.xaml中。

代码语言:javascript
复制
    <Style.Triggers>
            <EventTrigger>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="KeyDown">
                    <Behaviors:ExecuteCommandAction Command="{Binding TabKeyDownCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </EventTrigger>
    </Style.Triggers>

下面是我的ExecuteCommandAction类:

代码语言:javascript
复制
/// <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);
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-08-24 04:13:44

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

代码语言:javascript
复制
<Button>
    <i:Interaction.Triggers>
         <!-- ... -->
    </i:Interaction.Triggers>
</Button>

这个错误是由于没有在包含的EventTrigger上设置RoutedEvent而导致的,但是即使您设置了它,也会出现新的错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7166914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档