title: How to set/reset Three-state checkbox value in WPF
body: <p>I have a datagrid whose one of the header column is Three-state checkbox. The celltemplate for that column contains two state checkbox
  + AllItems CheckBox
    - Item1
    - Item2
    - Item3
     ..
  I wanted to use AllItems checkbox to select/unselect all items (item1,item2) which works fine. Next I wanted to set AllItems checkbox to intermediate state when not all items are selected/unselected. Similarly I wanted to set AllItems checkbox as checked/unchecked when all items get manually selected. </p>
  
  <p>Here is the code that I tried...</p>
  
  <pre><code>&lt;dg:DataGridTemplateColumn.HeaderTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;StackPanel x:Name="StackPanelForItemCheckbox" Orientation="Horizontal"&gt;
             &lt;CheckBox x:Name="AllItemSelectionCheckBox" HorizontalAlignment="Left" Cursor="Hand"  
                       IsChecked="{Binding IsAllItemChecked, Mode=TwoWay}"                                              
                       IsThreeState="True"  Checked="ItemSelectionCheckBox_Checked" 
                       Unchecked="ItemSelectionCheckBox_Unchecked"
                       Click="AllItemSelectionCheckBox_Click"&gt;
             &lt;TextBlock x:Name="ItemNameTextBlock" Text="Item" Margin="10,0,0,0"&gt;
             ......
  &lt;dg:DataGridTemplateColumn.CellTemplate&gt;
         &lt;DataTemplate x:Name="ItemCheckDataTemplate"&gt;                                
             &lt;StackPanel x:Name="ItemCheckBoxStackPanel" Orientation="Horizontal"&gt;                                    
                    &lt;CheckBox x:Name="itemCheckBox" Cursor="Hand" IsChecked="{Binding IsItemChecked, Mode=TwoWay}" Click="ItemSelectionCheckBox_Click"&gt;&lt;/CheckBox&gt;
                     &lt;TextBlock x:Name="ItemNameTextBlock" Text="{Binding Path=Item}"&gt; &lt;/TextBlock&gt;                                   
              &lt;/StackPanel&gt;
           &lt;/DataTemplate&gt;
  ...
  </code></pre>
  
  <p>"ItemSelectionCheckBox_Click" method looks for all three state (all-checked, none-checked, intermediate) and sets "IsAllItemChecked" property which is INotifyproperty. It does not work. Other alternative I may try is to find the "AllItems" element and set it from the code. Could not locate anything like that on web. There is few examples but is for TreeView and not the way I am trying. Any help?</p>
  
  <p><strong>PS>></strong></p>
  
  <h2>Updated with fix to close this post.</h2>
  
  <ul>
  <li><p>First thing I wanted was to allow "AllItemSelectionCheckBox" to have only two states (True, False) when manually selected.</p>
  
  <pre><code>private void AllItemSelectionCheckBox_Click(object sender, RoutedEventArgs e)
  {
      var cb = e.Source as CheckBox;
      if (!cb.IsChecked.HasValue)
          cb.IsChecked = false;  
  }
  </code></pre></li>
  <li>I wanted "AllItemSelectionCheckBox" checkbox to show three-state thru code.</li>
  <li>All check-box checked will cause its value to TRUE</li>
  <li>All check-box unchecked will cause its value to FALSE</li>
  <li>Any few selected will cause its value to NULL.</li>
  </ul>
  
  <p>Code Example following:</p>
  
  <pre><code>private void itemCheckBox_Checked(object sender, RoutedEventArgs e)
  { 
      DataGridRowsPresenter DGRPresenter = FindVisualChild&lt;DataGridRowsPresenter&gt;(DataGName1);
      if (DGRP == null || DGRP.Children == null)
          return null;
      foreach (object obj in UIEC)
      {
          DGR = obj as Microsoft.Windows.Controls.DataGridRow;
          UIC = DGR.Item as &lt;datagrid mapped data model&gt;;
          if (DGR.IsSelected == true)
              UIC.IsItemChecked = true;
          if (UIC.IsItemChecked == true)
                  NumberOfItemsChecked++;
      }
      if (NumberOfItemsChecked == myViewModelAllItems.Count)
      {
          allcheckbox.IsChecked = true;
      }
      else if (NumberOfItemsChecked &lt; myViewModelAllItems.Count)
      {
          allcheckbox.IsChecked = null;   //intermittent state
      }
  }
  </code></pre>
  
  <p>Updating NumberOfItemsChecked count globally did not work due to race condition corrupting the value outside.
  <p>
  <em>Note:</em> Above code is more like an example and may not work copying it directly. I can provide complete code with sample on request.</p>