title: Get the selected items of a ListView (MVVM, Caliburn.Micro)
body: <p><strong>What is the situation:</strong></p>
  
  <p>I'm working on a C# WPF application with Caliburn.Micro. I am using the MVVM pattern.</p>
  
  <p>I have a ListView with a ContentControl as ItemTemplate. The ListView's ItemsSource is bound to a List (ObservableCollection) of ViewModels in the corresponding ViewModel.</p>
  
  <pre><code>&lt;ListView ItemsSource="{Binding ViewModelList}" SelectionMode="Extended"&gt;
          &lt;ListView.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;ContentControl cal:View.Model="{Binding}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListView.ItemTemplate&gt;
          &lt;ListView.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;StackPanel Orientation="Horizontal" /&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ListView.ItemsPanel&gt;
      &lt;/ListView&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>What I want:</strong></p>
  
  <p>I want to get the selected items (all of them) of the ListView, but I don't know how without violating the MVVM pattern.</p>
  
  <p>It would be nice to have a property "IsSelected" in the ViewModels that are presented by the ContentControl and to bind that somehow to my ListView.</p>
  
  <p>Is it possible to do that or is there another/a better way?</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>It was easier than expected. I added a property <code>public bool IsSelected { get; set; }</code> in my ViewModel and put this inside the ListView Control:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
          &lt;Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}"&gt;
              &lt;Setter Property="IsSelected" Value="{Binding IsSelected}" /&gt;
          &lt;/Style&gt;
  &lt;/UserControl.Resources&gt;
  </code></pre>
  
  <p>Now I can get the selected items with:</p>
  
  <pre><code>foreach (var item in ViewModelList)
  {
       if (item.IsSelected)
       {
            // Do stuff
       }
  }
  </code></pre>