将带参数的命令附加到 KeyUp 事件

发布于 2024-09-09 11:07:35 字数 1470 浏览 2 评论 0原文

我目前正在将一个中型 WPF 项目转换为 MVVM,但遇到了一个我无法解决的问题。也许你可以帮我一下?

目标框架是.NET 3.5.1。

我有一个列表视图,它从底层视图模型获取其项目。该视图模型公开了一个命令,用于从列表视图中删除所选项目。因此,命令参数绑定到列表视图的 SelectedItems 属性。

<ListView ItemsSource="{Binding MyItems}"
          x:Name="MyListView"
          SelectionMode="Extended">
</ListView>
<Button x:Name="MyRemoveButton" 
        Content="Remove item" 
        Command="{Binding RemoveItemCommand}"
        CommandParameter="{Binding ElementName=MyListView, Path=SelectedItems}">

我的目的是不仅在按下按钮时执行此命令,而且在列表视图上触发 KeyUp 事件并且按下的键是“删除”时执行此命令。

当我在此示例中偶然发现交互触发器时,我即将找到解决方案:

http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1250325648481#c38674953 57686026904

现在这个演示的问题是命令参数是按下的键,但在我的例子中,我需要命令参数是 SelectedItems 属性,并且我需要命令仅在特定键上执行。

有没有什么方法可以在没有太多开销的情况下以 MVVM 方式做到这一点?

像这样的事情会很棒:

<i:Interaction.Triggers>
   <i:EventTrigger EventName="KeyUp">
      <local:CommandAction Command="{Binding RemoveItemCommand}"
                           CommandParameter={Binding ElementName=MyListView, Path=SelectedItems}
                           EventArgument="Key.Delete"/>
   </i:EventTrigger>
</i:Interaction.Triggers>

I am currently transforming a medium size WPF project to MVVM and I ran into a problem that I wasn't able to solve, yet. Maybe you could help me out?

The target framework is .NET 3.5.1.

I have a list view that gets its items from the underlying view model. That view model is exposing a command to remove the selected items from the list view. Therefore the command parameter is bound to the SelectedItems property of the list view.

<ListView ItemsSource="{Binding MyItems}"
          x:Name="MyListView"
          SelectionMode="Extended">
</ListView>
<Button x:Name="MyRemoveButton" 
        Content="Remove item" 
        Command="{Binding RemoveItemCommand}"
        CommandParameter="{Binding ElementName=MyListView, Path=SelectedItems}">

My intention is to execute this command not only when pressing a button, but also when the KeyUp event is fired on the list view and the pressed key is "delete".

I was close to finding the solution when I stumbled upon interaction triggers in this example:

http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1250325648481#c3867495357686026904

Now the problem with this demo is that the command parameter is the pressed key, but in my case I need the command parameter to be the SelectedItems property and I need the command to execute only on a specific key.

Is there any way to do this without much overhead and in the MVVM way?

Something like this would be awesome:

<i:Interaction.Triggers>
   <i:EventTrigger EventName="KeyUp">
      <local:CommandAction Command="{Binding RemoveItemCommand}"
                           CommandParameter={Binding ElementName=MyListView, Path=SelectedItems}
                           EventArgument="Key.Delete"/>
   </i:EventTrigger>
</i:Interaction.Triggers>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-09-16 11:07:35

要以 MVVM 方式执行此操作,您需要将 ListView 的“SelectedItems”属性绑定到 ViewModel,这样您就可以从命令中使用它,而无需通过 CommandParameter 传递它。

To do it in the MVVM way you need to bind "SelectedItems" property of the ListView to your ViewModel, so you could use it from your commands and wouldn't need to pass it via CommandParameter.

痴情 2024-09-16 11:07:35

您的分居要求有多严格?如果您没有使用 Blend 的设计人员,则将对 ViewModel 方法的调用放入代码隐藏中的 KeyUp 或 PreviewKeyUp 事件处理程序中。

How strict is your separation requirement? If you don't have designers using Blend, then put a call to a ViewModel method into the KeyUp or PreviewKeyUp event handler in your code-behind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文