Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding in multiple views to ObservableCollection of selected items (MVVM, SL5)
    primarykey
    data
    text
    <p>I'm using MVVM with Silverlight 5. What I want to achieve is to select items in a <code>ListBox</code> in one view (e.g. ListView.xaml) and display the selected items in a <code>ListBox</code> in another view (e.g. SelectionView.xaml).</p> <p>My selected items are in an <code>ObservableCollection&lt;MyItem&gt;</code> (called <code>SelectedItems</code>) that is created via attached behaviour as described <a href="https://stackoverflow.com/questions/1297643/sync-selecteditems-in-a-muliselect-listbox-with-a-collection-in-viewmodel">in this answer</a></p> <p>The problem seems to be that I am using two different XAML files. If I bind a <code>ListBox</code> to <code>SelectedItems</code> in the same XAML file where the selection happens, the items show up in another <code>ListBox</code> in the same view without problems. But in a different file, the <code>ListBox</code> stays empty.</p> <p>Both views use the same <code>ViewModel</code> as DataContext.</p> <p>I would be very happy about some pointers in the right direction. I'm new to SL so perhaps I'm missing something obvious.</p> <p>This is the code that works:</p> <p>ListView.xaml</p> <pre><code>&lt;UserControl x:Class="Silverlight5App.View.Content.ListView" xmlns:viewModel="clr-namespace:Silverlight5App.ViewModel" xmlns:behaviours="clr-namespace:Silverlight5App.Behaviours"&gt; &lt;UserControl.Resources&gt; &lt;viewModel:XYPlotViewModel x:Key="ViewModelTest" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModelTest}}"&gt; &lt;StackPanel&gt; &lt;ListBox ItemsSource="{Binding Path=XYPoints}" behaviours:SelectedItems.Items="{Binding SelectedItems}" Name="XYPointsListbox" SelectionMode="Extended" &gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Column="0" Margin="2" Text="{Binding id}" /&gt; &lt;TextBlock Grid.Column="1" Margin="2" Text="{Binding x}" /&gt; &lt;TextBlock Grid.Column="2" Margin="2" Text="{Binding y}" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;ListBox ItemsSource="{Binding SelectedItems}" Name="XYPointsListboxSelection"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Column="0" Margin="2" Text="{Binding id}" /&gt; &lt;TextBlock Grid.Column="1" Margin="2" Text="{Binding x}" /&gt; &lt;TextBlock Grid.Column="2" Margin="2" Text="{Binding y}" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>and this code just gives an empty listbox:</p> <p>SelectionView.xaml</p> <pre><code> &lt;UserControl x:Class="Silverlight5App.View.Content.SelectionView" xmlns:viewModel="clr-namespace:Silverlight5App.ViewModel" xmlns:behaviours="clr-namespace:Silverlight5App.Behaviours"&gt; &lt;UserControl.Resources&gt; &lt;viewModel:XYPlotViewModel x:Key="ViewModelTest" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModelTest}}" &gt; &lt;StackPanel&gt; &lt;ListBox ItemsSource="{Binding Path=SelectedItems}" Name="XYPointsListboxSelection2"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Column="0" Margin="2" Text="{Binding id}" /&gt; &lt;TextBlock Grid.Column="1" Margin="2" Text="{Binding x}" /&gt; &lt;TextBlock Grid.Column="2" Margin="2" Text="{Binding y}" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>PS: syntax highlighting is set to language-all: lang-xml but doesn't seem to work?</p>
    singulars
    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.
 

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