title: Silverlight Checkbox inside datagrid causing Trouble
body: <p>Hi all i came across a problem with the checkboxes inside the datagrid.</p>
  
  <p>First let me post my code of what i have achieved and then i will tell what's causing the trouble</p>
  
  <p>This is the code of my datagrid inside a child window</p>
  
  <pre><code>&lt;Controls:DataGrid x:Name="dgAthlete" Height="Auto" Width="Auto"
                                         IsReadOnly="True" AutoGenerateColumns="False"
                                         HorizontalAlignment="Stretch"
                                         HorizontalScrollBarVisibility="Disabled"
                                         ItemsSource="{Binding Data, ElementName=dds}"
                                     &gt;
              &lt;Controls:DataGrid.Columns&gt;
                  &lt;Controls:DataGridTemplateColumn Header="CheckBoxColumn"&gt;
                      &lt;Controls:DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;CheckBox  x:Name="cbAddAthlete"  IsChecked="{Binding IsAdded}" Tag="{Binding}"
                              IsEnabled="True" Checked="cbAddAthlete_Checked" Unchecked="cbAddAthlete_Unchecked" /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Controls:DataGridTemplateColumn.CellTemplate&gt;
                      &lt;Controls:DataGridTemplateColumn.CellEditingTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;CheckBox IsChecked="{Binding Path=IsAdded,Mode=TwoWay}"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Controls:DataGridTemplateColumn.CellEditingTemplate&gt;
                  &lt;/Controls:DataGridTemplateColumn&gt;
  
  
  
                  &lt;Controls:DataGridTemplateColumn&gt;
                      &lt;Controls:DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;Image Source="{Binding ImageFileName}"
                                                  Width="25" Height="25"
                                                  HorizontalAlignment="Stretch" /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Controls:DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/Controls:DataGridTemplateColumn&gt;
                  &lt;Controls:DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" /&gt;
                  &lt;Controls:DataGridTextColumn Header="MiddleName" Binding="{Binding MiddleName}"/&gt;
                  &lt;Controls:DataGridTextColumn Header="LastName" Binding="{Binding LastName}"/&gt;
                  &lt;Controls:DataGridTextColumn Header="Email"  Binding="{Binding Email}" /&gt;
                  &lt;Controls:DataGridTextColumn Header="DOB" Binding="{Binding BirthDate}"/&gt;
                  &lt;Controls:DataGridTextColumn Header="Phone" Binding="{Binding PhoneNumber}"/&gt;
                  &lt;Controls:DataGridTextColumn Header="Website" Binding="{Binding WebSite}"/&gt;
                  &lt;Controls:DataGridTextColumn Header="Team" Binding="{Binding TeamName}"/&gt;
                  &lt;Controls:DataGridTextColumn Header="Club" Binding="{Binding ClubName}"/&gt;
                  &lt;Controls:DataGridTextColumn Header="County" Binding="{Binding CountyName}"/&gt;
              &lt;/Controls:DataGrid.Columns&gt;
          &lt;/Controls:DataGrid&gt;
          &lt;Controls:DataPager x:Name="dpAthlete" PageSize="4"
                   HorizontalAlignment="Stretch" Source="{Binding Data, ElementName=dds}"
                   Width="Auto"/&gt;
      &lt;/StackPanel&gt;
      &lt;Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" /&gt;
      &lt;Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" /&gt;
  </code></pre>
  
  <p>The code behind for this is </p>
  
  <pre><code>public partial class AddAthletesToCompetition : ChildWindow
  {
      public ObservableCollection&lt;Athlete&gt; Athletes { get; set; }
      public int CompetitionId { get; set; }
      private PagedCollectionView pvcAthlete;
  
      public AddAthletesToCompetition(int competitionId)
      {
          InitializeComponent();
          CompetitionId = competitionId;
          LoadAthlete();
          Athletes = new ObservableCollection&lt;Athlete&gt;();
  
      }
  
      private void OKButton_Click(object sender, RoutedEventArgs e)
      {
          this.DialogResult = true;
      }
  
      private void CancelButton_Click(object sender, RoutedEventArgs e)
      {
          this.DialogResult = false;
      }
      private void LoadAthlete()
      {
          var context = new PublicServiceClient();
          context.GetAthletesNotInCompetitionCompleted += context_GetAthletesNotInCompetitionCompleted;
          context.GetAthletesNotInCompetitionAsync(CompetitionId);
      }
  
      void context_GetAthletesNotInCompetitionCompleted(object sender, GetAthletesNotInCompetitionCompletedEventArgs e)
      {
          if (e.Result != null)
          {
              pvcAthlete = new PagedCollectionView(e.Result.ToList());
              dgAthlete.ItemsSource = pvcAthlete;
              dpAthlete.Source = pvcAthlete;
          }
      }
  
  
      //Checkbox Checked Event Hanlder
      private void cbAddAthlete_Checked(object sender, RoutedEventArgs e)
      {
          var athlete = ((CheckBox)sender).Tag as AthleteBO;
          if (athlete != null)
          {
              var ath = new Athlete();
              ath.AthleteId = athlete.AthleteId;
              Athletes.Add(ath);
          }
      }
  
  
      //CheckBox unchecked Event Handler
      private void cbAddAthlete_Unchecked(object sender, RoutedEventArgs e)
      {
          var athlete = ((CheckBox)sender).Tag as AthleteBO;
          if (athlete != null)
          {
              var item = Athletes.First(i =&gt; i.AthleteId == athlete.AthleteId);
              Athletes.Remove(item);
          }
      }
  
  }
  </code></pre>
  
  <p>As you can see i am using paging everything works fine i.e check and uncheck events for the checkbox work fine when we are on the first page but let's say you checked the first and the second item in the grid on the first page , now as soon as i go to the next page what my grid is doing its retaining the previous view and checking by default the first and the second item on the second page which is not expected behaviour</p>
  
  <p>I went through various post on this issue and found that the problem is that the view is  injected by the datagrid but somehow i couldn't find any solution.</p>
  
  <p>What other's were talking about was refreshing the observable collection and again binding that to my grid.</p>
  
  <p>So i was a little confused as where to bind that and when to update the observable collection.</p>
  
  <p>I have posted whole of the code and whenever you reply please mention where exactly  should i do the changes for this to work.</p>
  
  <p>For a note i am using WCF and EF , so the list of athletes which i have here i will be sending this list to the WCF service where using EF it will be inserted into the database.</p>
  
  <p>I know it's not a bug in Silverlight but this added feature of grid virtualization is causing a trouble with me currently so there should aslo be some solution to this problem.</p>
  
  <p>Thanks </p>