Note that there are some explanatory texts on larger screens.

plurals
  1. POAttach command with parameter to KeyUp event
    primarykey
    data
    text
    <p>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?</p> <p>The target framework is .NET 3.5.1.</p> <p>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.</p> <pre><code>&lt;ListView ItemsSource="{Binding MyItems}" x:Name="MyListView" SelectionMode="Extended"&gt; &lt;/ListView&gt; &lt;Button x:Name="MyRemoveButton" Content="Remove item" Command="{Binding RemoveItemCommand}" CommandParameter="{Binding ElementName=MyListView, Path=SelectedItems}"&gt; </code></pre> <p>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".</p> <p>I was close to finding the solution when I stumbled upon interaction triggers in this example:</p> <p><a href="http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1250325648481#c3867495357686026904" rel="nofollow noreferrer">http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1250325648481#c3867495357686026904</a></p> <p>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. </p> <p>Is there any way to do this without much overhead and in the MVVM way?</p> <p>Something like this would be awesome:</p> <pre><code>&lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="KeyUp"&gt; &lt;local:CommandAction Command="{Binding RemoveItemCommand}" CommandParameter={Binding ElementName=MyListView, Path=SelectedItems} EventArgument="Key.Delete"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload