Xaml 中的事件触发器
我试图在我的风格中添加一个事件触发器,但由于某种原因它不起作用。 它向我抛出一个错误 {“值不能为空。\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Interactivity
功能不能这样使用,它不属于普通的Triggers
、EventTriggers
甚至样式本身。您可以在如下元素上使用它:该错误是由于未在包含的
EventTrigger
上设置RoatedEvent
引起的,但即使您确实设置了它,也会出现新的错误。Interactivity
functionality cannot be used like this, it does not belong to normalTriggers
,EventTriggers
or even styles themselves. You can use it on elements like this:The error is caused by not setting
RoutedEvent
on the encompassingEventTrigger
, but even if you did set it, new errors would follow.