EventTrigger in Xaml
本文关键字:Xaml in EventTrigger | 更新日期: 2023-09-27 18:06:23
我试图在我的风格中添加一个事件触发器,但由于某种原因它不工作。它向我抛出{"值不能为空"的错误。'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);
}
}
}
}
Interactivity
功能不能这样使用,它不属于正常的Triggers
、EventTriggers
甚至样式本身。你可以在这样的元素上使用:
<Button>
<i:Interaction.Triggers>
<!-- ... -->
</i:Interaction.Triggers>
</Button>
这个错误是由于没有在包含的EventTrigger
上设置RoutedEvent
而引起的,但是即使您设置了它,也会出现新的错误。