Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I find J King answer quite good, but I will like to shed some more light and options never the less.</p> <p>In the comment you asked about what happens if it's not the same view?<br> Here's an implementation I have: </p> <pre><code>&lt;ListBox x:Name="YourListView" ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding SelectedItemObject, UpdateSourceTrigger=PropertyChanged}" ToolTip="Double click to edit" &gt; &lt;ListBox.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header ="Edit me" Command="{Binding Edit_Command}" CommandParameter="{Binding SelectedItemObject}" /&gt; &lt;MenuItem Header ="Delete me" Command="{Binding Delete_Command}" CommandParameter="{Binding SelectedItemObject}" /&gt; &lt;/ContextMenu&gt; &lt;/ListBox.ContextMenu&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="MouseDoubleClick"&gt; &lt;Command:EventToCommand Command="{Binding Edit_Command}" CommandParameter="{Binding ElementName=YourListView, Path=SelectedItem}" /&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/ListBox&gt; </code></pre> <p>Notice that either double clicking on an object in that list, or right clicking and selecting one of the two options (namely <code>edit</code> and <code>delete</code>), will Call a command with a paramater.</p> <p>Now, when you use the <code>ContextMenu</code> since you right clicked and object, it's selected, and you can just send it.</p> <p>In the case of the double click, I'm using the name of the listbox to get the item.</p> <p>From here what I'll do in the viewmodel is something like:</p> <pre><code>private void Execute_Edit(object param) { var your_object = (cast_to_your_type)param; Messenger.Default.Send(new SwitchView(new SomeViewModel(_dataService,your_object))); } </code></pre> <p>The ICommand will call <code>Execute_Edit</code>, which in turn will send a message using the Messenger.</p> <p>This is how I defined <code>SwitchView</code>:</p> <pre><code>/// &lt;summary&gt; /// Used as message, to switch the view to a different one. /// &lt;/summary&gt; public class SwitchView { public SwitchView(MyViewModelBase viewmodel) { ViewModel = viewmodel; } public MyViewModelBase ViewModel { get; set; } } </code></pre> <p>My MainWindow is registered to listen to those messages,and we know what to change to (obviously, to the view model given : <code>SomeViewModel</code>). The <code>SwitchView</code> on the main class will change the ViewModel property to the property the message passed.</p> <p>Here's what I have in my main view:</p> <pre><code>&lt;Border &gt; &lt;ContentControl Content="{Binding Current_VM}" /&gt; &lt;/Border&gt; </code></pre> <p>So, whatever the <code>Current_VM</code> property is set to, will show you that view.</p> <p>Hope that helped :)</p>
 

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