C# 传递KeyUp作为参数WPF命令绑定文本框
我有一个文本框KeyUp事件触发器连接到WPF中的一个命令。 我需要传递作为命令参数按下的实际键 该命令执行得很好,但处理它的代码需要知道实际按下的键(请记住,这可能是一个回车键或任何东西,而不仅仅是一个字母,因此我无法从TextBox.text中获取它) 我不知道怎么做。 XAML: XAML:C# 传递KeyUp作为参数WPF命令绑定文本框,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个文本框KeyUp事件触发器连接到WPF中的一个命令。 我需要传递作为命令参数按下的实际键 该命令执行得很好,但处理它的代码需要知道实际按下的键(请记住,这可能是一个回车键或任何东西,而不仅仅是一个字母,因此我无法从TextBox.text中获取它) 我不知道怎么做。 XAML: XAML: 我认为InvokeCommandAction不可能做到这一点,但您可以快速创建自己的行为,大致如下所示: public class KeyUpWithArgsBehavior : Behavior&l
我认为InvokeCommandAction不可能做到这一点,但您可以快速创建自己的
行为,大致如下所示:
public class KeyUpWithArgsBehavior : Behavior<UIElement>
{
public ICommand KeyUpCommand
{
get { return (ICommand)GetValue(KeyUpCommandProperty); }
set { SetValue(KeyUpCommandProperty, value); }
}
public static readonly DependencyProperty KeyUpCommandProperty =
DependencyProperty.Register("KeyUpCommand", typeof(ICommand), typeof(KeyUpWithArgsBehavior), new UIPropertyMetadata(null));
protected override void OnAttached()
{
AssociatedObject.KeyUp += new KeyEventHandler(AssociatedObjectKeyUp);
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.KeyUp -= new KeyEventHandler(AssociatedObjectKeyUp);
base.OnDetaching();
}
private void AssociatedObjectKeyUp(object sender, KeyEventArgs e)
{
if (KeyUpCommand != null)
{
KeyUpCommand.Execute(e.Key);
}
}
}
这样你就应该收到键
作为命令的参数。我使用MVVMLight,可以传递事件参数
比如:嘿,对不起,我现在意识到我想要完整的事件参数(不仅仅是按下的键),但是你的答案对按键有效,关于如何获得完整事件参数的任何提示?是的,这对我来说很有用-非常感谢(只需执行.Execute(e)而不是.Execute(e.key)即可提供完整的事件参数。太棒了!事件不会传递给特殊的键,如enter、tab、up、down等。我如何为它们获取KeyEvent?仅供参考,我使用的是KeyDown而不是KeyUp,并使用一个自定义文本框。它适用于字母和数字等其他按钮。
<TextBox Height="23" Name="TextBoxSelectionSearch" Width="148" Tag="Enter Selection Name" Text="{Binding Path=SelectionEditorFilter.SelectionNameFilter,UpdateSourceTrigger=PropertyChanged}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction Command="{Binding SelectionEditorSelectionNameFilterKeyUpCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
public class KeyUpWithArgsBehavior : Behavior<UIElement>
{
public ICommand KeyUpCommand
{
get { return (ICommand)GetValue(KeyUpCommandProperty); }
set { SetValue(KeyUpCommandProperty, value); }
}
public static readonly DependencyProperty KeyUpCommandProperty =
DependencyProperty.Register("KeyUpCommand", typeof(ICommand), typeof(KeyUpWithArgsBehavior), new UIPropertyMetadata(null));
protected override void OnAttached()
{
AssociatedObject.KeyUp += new KeyEventHandler(AssociatedObjectKeyUp);
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.KeyUp -= new KeyEventHandler(AssociatedObjectKeyUp);
base.OnDetaching();
}
private void AssociatedObjectKeyUp(object sender, KeyEventArgs e)
{
if (KeyUpCommand != null)
{
KeyUpCommand.Execute(e.Key);
}
}
}
<TextBox Height="23" Name="TextBoxSelectionSearch" Width="148" Tag="Enter Selection Name" Text="{Binding Path=SelectionEditorFilter.SelectionNameFilter,UpdateSourceTrigger=PropertyChanged}" >
<i:Interaction.Behaviors>
<someNamespace:KeyUpWithArgsBehavior
KeyUpCommand="{Binding SelectionEditorSelectionNameFilterKeyUpCommand}" />
</i:Interaction.Behaviors>
</TextBox>