Silverlight / WPF - Problem with Wpf Datagrid template column

Asked By prabhakar madduri on 01-Dec-09 06:16 AM
Hi,
        Iam using datagrid template column to checkboxes and i want to check all the checkboxes by clicking a button my code is as below
Xaml code:
<data:DataGrid x:name:dg>
  <data:DataGridTemplateColumn  Header="SNO"   Width="50" CanUserResize="False" CanUserReorder="False"  >
                                <data:DataGridTemplateColumn.CellTemplate>
                                   <DataTemplate>
                                 
                                       <CheckBox x:Name="chk" Content="{Binding SNO}"   HorizontalAlignment="Center"     VerticalAlignment="Center" IsChecked="{Binding Allow, Mode=TwoWay}"  />
                                                                </DataTemplate>
                                </data:DataGridTemplateColumn.CellTemplate>
                            </data:DataGridTemplateColumn>
</data:DataGrid>



c# code
  private void btn_chkall_Click(object sender, RoutedEventArgs e)
        {
          

            for (int i = 0; i < dg.Items.Count ; i++)
            {
                dg.ScrollIntoView(this.list, dg.Columns[1]);
                dg.SelectedIndex = i;
                CheckBox Check = this.dg.Columns[1].GetCellContent(dg.SelectedItem) as CheckBox;
              

                Check.IsChecked = true;
            }

          
        }





But i am getting error in runtime as below
Object reference not set to an instance of an object:Null Reference exeception
Can Anybody Help me to selectall checkboxes
Thanks in Advance.


Update the data binding source. - Michael Detras replied to prabhakar madduri on 01-Dec-09 07:55 AM

Hi, since you are using data binding already, you could directly update the value of the source of the binding expression in the IsChecked property. Thus, you don't need to get the check boxes and set their IsChecked properties to true.