Problems with DataGrid checkbox for WPF Toolkit

I really hope someone can help me here. I have a DataGrid in my program that has a checkbox column. The ItemsSource element for a DataGrid is a DataSet loaded programmatically. When I select a couple of elements in a DataGrid and then scroll through them, I get a very strange behavior. For example, when I check two of CheckBoxes, it tells me that I have "2 selected", but if I scroll up or down in the DataGrid, the number will change. If I go back to the starting position, it will return to “2 selected”. Oddly enough, it seems like it is raising Checked / Unchecked events when I view the field ... very strange ...

I have the following definition at the top of my code:

private DataSet MyDataSet;
int selected_count = 0;

Then I have the following code in my method for loading information into a DataSet:

MyDataSet = new DataSet();
DataTable tempDataTable = new DataTable();
MyDataSet.Tables.Add(tempDataTable);

DataColumn tempCol = new DataColumn("Checked", typeof(bool));
tempDataTable.Columns.Add(tempCol);

for (int i = 0; i < 50; i++)
{
    DataRow tempRow = tempDataTable.NewRow();
    tempDataTable.Rows.Add(tempRow);
    tempRow["Checked"] = false;
}

MyList.ItemsSource = MyDataSet.Tables[0].DefaultView;

I have an IsChecked property associated with a DataColumn named "Checked" using the following XAML:

<dtgrd:DataGrid x:Name="MyList" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeRows="False" HeadersVisibility="Column" SelectionUnit="FullRow" HorizontalGridLinesBrush="#FF688CAF" VerticalGridLinesBrush="#FF688CAF">
    <dtgrd:DataGrid.Columns>
        <dtgrd:DataGridTemplateColumn x:Name="CheckCol" CanUserSort="True" CanUserResize="False">
            <dtgrd:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Name="MyCheckBox" IsChecked="{Binding Checked}" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="MyCheckBox_Checked" Unchecked="MyCheckBox_Unchecked" />
                </DataTemplate>
            </dtgrd:DataGridTemplateColumn.CellTemplate>
        </dtgrd:DataGridTemplateColumn>
    </dtgrd:DataGrid.Columns>
</dtgrd:DataGrid>

Then I have the following events triggered by checking / unchecking one of the checkboxes:

private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
    selected_count++;
    TxtSelectedCount.Text = "" + selected_count + " selected";
}

private void MyCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    selected_count--;
    TxtSelectedCount.Text = "" + selected_count + " selected";
}

I also tried other things, but getting different errors. For example, I removed Binding from XAML code and tried to install it programmatically using the following Checked / Uncheck events:

private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
    DataRow tempRow = MyDataSet.Tables[0].Rows[MyList.Items.IndexOf(MyList.SelectedItems[0])];
    tempRow["Checked"] = true;

    selected_count++;
    TxtSelectedCount.Text = "" + selected_count + " selected";
}

private void MyCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    DataRow tempRow = MyDataSet.Tables[0].Rows[MyList.Items.IndexOf(MyList.SelectedItems[0])];
    tempRow["Checked"] = false;

    selected_count--;
    TxtSelectedCount.Text = "" + selected_count + " selected";
}

When I use this code, the number of elements checked remains the same, but the actual checks move to the various elements as I scroll.

I honestly have no idea what is happening, but it is very unpleasant! Any help would be greatly appreciated!

+2
1

. . http://blogs.msdn.com/b/vinsibal/archive/2008/05/14/recycling-that-item-container.aspx. WPF , , Checked Unchecked , .

,   VirtualizingStackPanel.VirtualizationMode="Standard" dtgrd:DataGrid. , VirtualizingStackPanel.IsVirtualizing="False".

, , . DataTable.ColumnChanged DataTable.

+7

All Articles