title: WPF : Select/Deselect ListView's Data items on click of Select All checkbox control
body: <p>I am working on a WPF application where I have to bind three different <code>ListViews</code> grouped inside a <code>GroupBox</code> container.</p>
  
  <p>This is the XAML for the display:</p>
  
  <pre><code>&lt;GroupBox Header="Zone2 Sensors" Height="250" Name="grpBoxZone2" Width="380" Margin="25,248,90,660" Grid.Column="5" Grid.ColumnSpan="2" Foreground="#FF1E5EE6"&gt;
      &lt;Grid Height="250" Width="375" MinHeight="250"&gt;
          &lt;Label Content="Strain" Width="40" Name="labelZone2S" Foreground="#FF1E5EE6" Margin="44,1,291,227" /&gt;
          &lt;Label Content="Accelerometer" Width="79" Name="labelZone2A" Foreground="#FF1E5EE6" Margin="132,1,164,227" /&gt;
          &lt;Label Content="Tilt" Name="labelZone2T" Foreground="#FF1E5EE6" Margin="264,1,82,227" /&gt;
          &lt;ListView Margin="25,42,268,49" Name="lst2" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" BorderThickness="0" SelectionChanged="lst2_SelectionChanged"&gt;
              &lt;ListView.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Border Padding="5" Background="#FF83A2DA" BorderThickness="1" BorderBrush="Black"&gt;
                          &lt;CheckBox Name="CheckBoxs1" IsChecked="{Binding IsSelected}" Click="CheckBoxs1_Click" Unchecked="CheckBoxs1_Unchecked"&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;TextBlock Text="{Binding ObjectData}"&gt;&lt;/TextBlock&gt;
                              &lt;/StackPanel&gt;
                          &lt;/CheckBox&gt;
                      &lt;/Border&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListView.ItemTemplate&gt;
          &lt;/ListView&gt;
          &lt;ListView Margin="134,42,159,111" Name="lstZone2A" ItemsSource="{Binding}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" BorderThickness="0" SelectionChanged="lstZone2A_SelectionChanged"&gt;
              &lt;ListView.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Border Padding="5" Background="#FF83A2DA" BorderThickness="1" BorderBrush="Black"&gt;
                          &lt;CheckBox Name="ChkZone2A" IsChecked="{Binding IsSelected}" Checked="ChkZone2A_Checked" Unchecked="ChkZone2A_Unchecked"&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;TextBlock Text="{Binding ObjectData}"&gt;&lt;/TextBlock&gt;
                              &lt;/StackPanel&gt;
                          &lt;/CheckBox&gt;
                      &lt;/Border&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListView.ItemTemplate&gt;
          &lt;/ListView&gt;
          &lt;ListView Margin="249,42,44,156" Name="lstZone2T" ItemsSource="{Binding}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" BorderThickness="0" SelectionChanged="lstZone2T_SelectionChanged"&gt;
              &lt;ListView.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Border Padding="5" Background="#FF83A2DA" BorderThickness="1" BorderBrush="Black"&gt;
                          &lt;CheckBox Name="ChkZone2T" IsChecked="{Binding IsSelected}" Checked="ChkZone2T_Checked" Unchecked="ChkZone2T_Unchecked"&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;TextBlock Text="{Binding ObjectData}"&gt;&lt;/TextBlock&gt;
                              &lt;/StackPanel&gt;
                          &lt;/CheckBox&gt;
                      &lt;/Border&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListView.ItemTemplate&gt;
          &lt;/ListView&gt;
          &lt;CheckBox Content="Select ALL Sensors" Margin="14,21,248,213" Name="chkZone2SelectAll" Checked="chkZone2SelectAll_Checked" Unchecked="chkZone2SelectAll_Unchecked" /&gt;
      &lt;/Grid&gt;
  &lt;/GroupBox&gt;
  </code></pre>
  
  <p>This is the C# code which I use to check and uncheck items:</p>
  
  <pre><code>private void chkZone2SelectAll_Checked(object sender, RoutedEventArgs e)
  {
      try
      {
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i &lt; lst2.Items.Count; i++)
          {
                  // Get a all list items from listbox
                  ListBoxItem ListBoxItemObj = (ListBoxItem)lst2.ItemContainerGenerator.ContainerFromItem(lst2.Items[i]);
                  //bool check = ListBoxItemObj.HasContent;
                  // find a ContentPresenter of that list item.. [Call FindVisualChild Method]
                  ContentPresenter ContentPresenterObj = FindVisualChild&lt;ContentPresenter&gt;(ListBoxItemObj);
  
                  // call FindName on the DataTemplate of that ContentPresenter
                  DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate;
                  CheckBox Chk = (CheckBox)DataTemplateObj.FindName("CheckBoxs1", ContentPresenterObj);
                  Chk.IsChecked = true;
          }
          for (int i = 0; i &lt; lstZone2A.Items.Count; i++)
          {
              // Get a all list items from listbox
              ListBoxItem ListBoxItemObj = (ListBoxItem)lstZone2A.ItemContainerGenerator.ContainerFromItem(lstZone2A.Items[i]);
  
              // find a ContentPresenter of that list item.. [Call FindVisualChild Method]
              ContentPresenter ContentPresenterObj = FindVisualChild&lt;ContentPresenter&gt;(ListBoxItemObj);
  
              // call FindName on the DataTemplate of that ContentPresenter
              DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate;
              CheckBox Chk = (CheckBox)DataTemplateObj.FindName("ChkZone2A", ContentPresenterObj);
              Chk.IsChecked = true;
          }
          for (int i = 0; i &lt; lstZone2T.Items.Count; i++)
          {
              // Get a all list items from listbox
              ListBoxItem ListBoxItemObj = (ListBoxItem)lstZone2T.ItemContainerGenerator.ContainerFromItem(lstZone2T.Items[i]);
  
              // find a ContentPresenter of that list item.. [Call FindVisualChild Method]
              ContentPresenter ContentPresenterObj = FindVisualChild&lt;ContentPresenter&gt;(ListBoxItemObj);
  
              // call FindName on the DataTemplate of that ContentPresenter
              DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate;
              CheckBox Chk = (CheckBox)DataTemplateObj.FindName("ChkZone2T", ContentPresenterObj);
              Chk.IsChecked = true;
          }
          foreach (TripInfo cbObject in lst2.Items)
          {
              cbObject.IsSelected = true;
          }
          foreach (TripInfo cbObject in lstZone2A.Items)
          {
              cbObject.IsSelected = true;
          }
          foreach (TripInfo cbObject in lstZone2T.Items)
          {
              cbObject.IsSelected = true;
          }
          foreach (TripInfo cbObject in lst2.Items)
          {
              if (cbObject.IsSelected)
              {
                  if (SelectedSensser != null)
                  {
                      if (SelectedSensser.Contains(cbObject.ObjectData.ToString())) { }
                      else
                      {
                          sb.AppendFormat("{0}, ", cbObject.ObjectData.ToString());
                      }
                  }
                  else
                  {
                      sb.AppendFormat("{0}, ", cbObject.ObjectData.ToString());
                  }
                  SelectedSensser += sb.ToString().Trim();
                  sb.Clear();
              }
          }
          foreach (TripInfo cbObject in lstZone2A.Items)
          {
              if (cbObject.IsSelected)
              {
                  if (SelectedSensser != null)
                  {
                      if (SelectedSensser.Contains(cbObject.ObjectData.ToString())) { }
                      else
                      {
                          sb.AppendFormat("{0}, ", cbObject.ObjectData.ToString());
                      }
                  }
                  else
                  {
                      sb.AppendFormat("{0}, ", cbObject.ObjectData.ToString());
                  }
                  SelectedSensser += sb.ToString().Trim();
                  sb.Clear();
              }
          }
          foreach (TripInfo cbObject in lstZone2T.Items)
          {
              if (cbObject.IsSelected)
              {
                  if (SelectedSensser != null)
                  {
                      if (SelectedSensser.Contains(cbObject.ObjectData.ToString())) { }
                      else
                      {
                          sb.AppendFormat("{0}, ", cbObject.ObjectData.ToString());
                      }
                  }
                  else
                  {
                      sb.AppendFormat("{0}, ", cbObject.ObjectData.ToString());
                  }
                  SelectedSensser += sb.ToString().Trim();
                  sb.Clear();
              }
          }
      }
      catch (Exception ex) { throw ex; }
  }
  
  private ChildControl FindVisualChild&lt;ChildControl&gt;(DependencyObject DependencyObj) where ChildControl : DependencyObject
  {
      int count = VisualTreeHelper.GetChildrenCount(DependencyObj);
      for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(DependencyObj); i++)
      {
          DependencyObject Child = VisualTreeHelper.GetChild(DependencyObj, i);
  
          if (Child != null &amp;&amp; Child is ChildControl)
          {
              return (ChildControl)Child;
          }
          else
          {
              ChildControl ChildOfChild = FindVisualChild&lt;ChildControl&gt;(Child);
  
              if (ChildOfChild != null)
              {
                  return ChildOfChild;
              }
          }
      }
      return null;
  }
  
  public class TripInfo
  {
  public TripInfo(string objectData) { ObjectData = objectData; }
  
  public TripInfo(bool isSelected, string objectData)
  {
      IsSelected = isSelected;
  
      ObjectData = objectData;
  }
  
  public Boolean IsSelected
  { get; set; }
  public String ObjectData
  { get; set; }
  
  }
  </code></pre>
  
  <p>However, this gives me the following error when it reaches the 8th index, but the <code>ListItems</code> count shows 10 elements:</p>
  
  <pre><code>Value cannot be null.
  Parameter name: element
  </code></pre>