Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct way to pass data between view models?
    text
    copied!<p>I've just started with MVVM and I have been reading up on it and doing some examples. I've managed to create an application that will read from the database and then populate into a listbox. I am having difficulty in trying to link up the selected item into another view and then do a bit of processing in that views viewModel. Please can somebody explain to me the correct way to get the currently selected item from view1 listbox and then on view2 label just to output the selected item?</p> <p>Here is my XAML:</p> <pre><code>&lt;local:SecondView Margin="499,30,0,20"&gt; &lt;local:SecondView.DataContext&gt; &lt;v:MainViewModel /&gt; &lt;/local:SecondView.DataContext&gt; &lt;/local:SecondView&gt; &lt;Button Height="22" HorizontalAlignment="Left" Margin="8,4,0,0" Name="button1" VerticalAlignment="Top" Width="48" Command="{Binding Path=GetDataCommand}"&gt;Button&lt;/Button&gt; &lt;ListBox ItemsSource="{Binding Path=FileData}" SelectedItem="{Binding dr}" Height="330" HorizontalAlignment="Left" Margin="149,30,0,0" Name="listBox1" VerticalAlignment="Top" Width="250" DisplayMemberPath="DDFName" /&gt; </code></pre> <p>This piece of code is in my viewModel1:</p> <pre><code>private DataRowView _dr; public DataRowView dr { get{ return _dr; } set { _dr = value; OnPropertyChanged("dr");} } </code></pre> <p>I want to somehow get viewModel2 to get the new value of dr (which is the item selected in view1 listbox) and then on view2 I want to show some details</p> <p>Thanks in advance!</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