|ZYCWPF| Interaction.Triggers中的EventTrigger中的KeyDown如何将KeyEventArgs进行传参,谢谢

javamy010 2012-11-06 02:09:24
如我有

<TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding OnUserNameKeyDown}" CommandParameter="???" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>


public ICommand OnUserNameKeyDown { get; set; }
/// <summary>
/// 绑定事件
/// </summary>
void BindEvent()
{
this.OnUserNameKeyDown = new DelegateCommand<KeyEventArgs>(UserNameKeyDown);
}
void UserNameKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Login(null);
}
}

在XAML中如何绑定这个KeyDown的Event参数
谢谢
...全文
688 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2012-11-06
  • 打赏
  • 举报
回复
ls 回答的很好,学习了。 另,MvvmLight 已经用 PassEventArgsToCommand 封装了。
  
 xmlns:gmc="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
 ...

<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyDown">
      <gmc:EventToCommand PassEventArgsToCommand="True" Command="{Binding KeyDownCommand}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
jshi123 2012-11-06
  • 打赏
  • 举报
回复
据我所知InvokeCommandAction无法传这个事件参数。 你可以直接用CallMethodAction:

    <TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="KeyDown">
                <ei:CallMethodAction TargetObject="{Binding}" MethodName="UserNameKeyDown" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
UserNameKeyDown要改成标准的事件处理方法的形式:

	public void UserNameKeyDown(object sender, KeyEventArgs e)
	{
		if (e.Key == Key.Enter)
		{
			Login(null);
		}
	}
当然模仿InvokeCommandAction自己写一个可以传参的action也很容易:

	public class InvokeEventCommand : TriggerAction<DependencyObject>
	{
		public static readonly DependencyProperty CommandProperty = 
			DependencyProperty.Register("Command", typeof(ICommand), typeof(InvokeEventCommand));

		public ICommand Command
		{
			get { return (ICommand)GetValue(CommandProperty); }
			set { SetValue(CommandProperty, value); }
		}

		protected override void Invoke(object parameter)
		{
			if (Command != null && Command.CanExecute(parameter))
				Command.Execute(parameter);
		}
	}
前台调用:

    <TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="KeyDown">
                <local:InvokeEventCommand Command="{Binding OnUserNameKeyDown}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
还是一种方法是做一个attached propety, 这样可以直接写在TextBox标记里,不必用Interaction.Triggers绕个圈子:

	public class CommandAction
	{
		public static DependencyProperty KeyDownProperty =
			DependencyProperty.RegisterAttached("KeyDown", typeof(ICommand), typeof(CommandAction),
												new UIPropertyMetadata(KeyDownCommandChanged));

		public static void SetKeyDown(UIElement target, bool value)
		{
			target.SetValue(KeyDownProperty, value);
		}

		private static void KeyDownCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			var textbox = target as TextBox;
			if (textbox == null)
				throw new InvalidOperationException("This behavior can be attached to TextBox only.");
			if (e.NewValue != null && e.OldValue == null)
				textbox.KeyDown +=TextboxKeyDown;
			else if (e.NewValue == null && e.OldValue != null)
				textbox.KeyDown -= TextboxKeyDown;
		}

		static void TextboxKeyDown(object sender, KeyEventArgs e)
		{
			var textbox = (TextBox) sender; 
			var command = (ICommand)textbox.GetValue(KeyDownProperty);
			command.Execute(e);
		}
	}
前台:
    <TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}" 
        local:CommandAction.KeyDown="{Binding OnUserNameKeyDown}"/>
javamy010 2012-11-06
  • 打赏
  • 举报
回复
UP。。。。。。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧