title: in Silverlight DataGrid, CheckBox's DataContext sometimes null when Checked/Unchecked
body: <p>I have a DataGrid.  One of the columns is a template with a CheckBox in it.  When the Checked or Unchecked events trigger, (it happens with both) the CheckBox's DataContext is sometimes null, which causes my code to error.  It seems to be null most often if the mouse is moving while you press and release the button quickly (it's intermittent).</p>
  
  <p>I listened for changes to the DataContext of the CheckBox by making views:ListenCheckBox (extends CheckBox) and attaching a binding, and it's never set <em>to</em> null, but it is set from null to a Task at times I wouldn't expect, i.e. after the DataGrid has been totally generated and you're checking/unchecking boxes.  Immediately after the [un]checked event runs with a null DataContext, I get the notification that shows the DataContext changed from null to a Task, so it appears that when I get a null DataContext, it's because it hadn't actually set the DataContext by the time it ran the Checked/Unchecked event.</p>
  
  <p>Also, I added Tag="{Binding}" to the CheckBox for debugging.  The Tag is not null (i.e.  it has the proper object) more often than the DataContext, but still not all the time.</p>
  
  <p>Here are the relevant bits of the XAML code:</p>
  
  <pre><code>&lt;navigation:Page.Resources&gt;
      &lt;sdk:DataGridTemplateColumn x:Key="DeleteOrPrintSelect" Header="Delete Or Print Notes Selection"&gt;
          &lt;sdk:DataGridTemplateColumn.CellTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;views:ListenCheckBox IsChecked="{Binding DeleteOrPrintNotesSelection, Mode=TwoWay}" Checked="DeletePrintNotesCheckBox_Changed" Unchecked="DeletePrintNotesCheckBox_Changed" HorizontalAlignment="Center" VerticalAlignment="Center" Tag="{Binding}" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/sdk:DataGridTemplateColumn.CellTemplate&gt;
      &lt;/sdk:DataGridTemplateColumn&gt;
  &lt;/navigation:Page.Resources&gt;
  
  
      &lt;sdk:DataGrid x:Name="dataGrid1" Grid.Column="1" Grid.Row="2" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn"&gt;
          &lt;sdk:DataGrid.RowGroupHeaderStyles&gt;
              [removed]
          &lt;/sdk:DataGrid.RowGroupHeaderStyles&gt;
      &lt;/sdk:DataGrid&gt;
  </code></pre>
  
  <p>And the relevant code behind:</p>
  
  <pre><code>        // Create a collection to store task data.
          ObservableCollection&lt;Task&gt; taskList = new ObservableCollection&lt;Task&gt;();
          [code adding Tasks to taskList removed]
  
          PagedCollectionView panelListView = new PagedCollectionView(taskList);
          this.dataGrid1.ItemsSource = panelListView;
      }
  
      private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
      {
          if (e.PropertyName == "DeleteOrPrintNotesSelection")
          {
              e.Column = Resources["DeleteOrPrintSelect"] as DataGridTemplateColumn;
          }
          else
          {
              e.Column.IsReadOnly = true;
          }
      }
  
      private void DeletePrintNotesCheckBox_Changed(object sender, RoutedEventArgs e)
      {
          try
          {
              var cb = sender as CheckBox;
              var t = cb.DataContext as Task;
              t.DeleteOrPrintNotesSelection = cb.IsChecked == true;
              PagedCollectionView pcv = this.dataGrid1.ItemsSource as PagedCollectionView;
              ObservableCollection&lt;Task&gt; taskList = pcv.SourceCollection as ObservableCollection&lt;Task&gt;;
              bool anySelected = taskList.Any(x =&gt; x.DeleteOrPrintNotesSelection);
              this.btnPrint.IsEnabled = anySelected;
              this.btnDelete.IsEnabled = anySelected;
          }
          catch (Exception ex)
          {
              ErrorMessageBox.Show("recheck", ex, this);
          }
      }
  </code></pre>
  
  <p>Any ideas? Thanks in advance.</p>