title: ListBox Selected Items in WPF using MVVM
body: <p>I have a <code>ListBox</code> in WPF and it has <code>CheckBox</code> items inside. I'm splitting the checked checkboxes with the following code in XAML codebehind.</p>
  
  <pre class="lang-csharp prettyprint-override"><code>StringBuilder sbMonths = new StringBuilder();
  StringBuilder sbMonthNames = new StringBuilder();
  
  string months;
  string monthNames;
  
  foreach (var item in ListBox.Items)
  {
      if (item is CheckBox)
      {
          var chk = item as CheckBox;
  
          if ((bool)chk.IsChecked)
          {
              sbMonths.Append(chk.Tag);
              sbMonths.Append(',');
  
              sbMonthNames.Append(chk.Content.ToString());
              sbMonthNames.Append('-');
          }
      }
  
  }
  
  months = sbMonths.ToString();
  monthNames = sbMonthNames.ToString();
  int lastIndexOfMonths = months.LastIndexOf(',');
  int lastIndexOfMonthNames = monthNames.LastIndexOf('-');
  
  if (lastIndexOfMonths &gt; -1)
  {
      months = months.Remove(lastIndexOfMonths);
      monthNames = monthNames.Remove(lastIndexOfMonthNames);
      .... I do more here
  }
  </code></pre>
  
  <p>The thing is that I have no idea how to do this using MVVM. Would you provide some solutions?</p>
  
  <p><strong>XAML:</strong>
  </p>
  
  <pre><code>&lt;ListBox Name="ListBox" HorizontalAlignment="Left" Height="149" Margin="23,47,0,0" VerticalAlignment="Top" Width="210"&gt;
      &lt;ComboBoxItem Content="-- Ay Seçiniz --"/&gt;
      &lt;CheckBox Content="Ocak" Tag="1"/&gt;
      &lt;CheckBox Content="Şubat" Tag="2"/&gt;
      &lt;CheckBox Content="Mart" Tag="3"/&gt;
      &lt;CheckBox Content="Nisan" Tag="4"/&gt;
      &lt;CheckBox Content="Mayıs" Tag="5"/&gt;
      &lt;CheckBox Content="Haziran" Tag="6"/&gt;
      &lt;CheckBox Content="Temmuz" Tag="7"/&gt;
      &lt;CheckBox Content="Ağustos" Tag="8"/&gt;
      &lt;CheckBox Content="Eylül" Tag="9"/&gt;
      &lt;CheckBox Content="Ekim" Tag="10"/&gt;
      &lt;CheckBox Content="Kasım" Tag="11"/&gt;
      &lt;CheckBox Content="Aralık" Tag="12"/&gt;
  &lt;/ListBox&gt;
  </code></pre>