CheckBox binding in ListView - Multiple events!
I'm binding a checkbox in a ListView to a boolean column in a DataTable, and catching the Checked & Unchekced events to add & remove rows from the table (this is to create a multi-select, multi-column TreeListView, because the WPF TreeView is so basic).
Here's the strange behavior:
When I handle the Checked event and insert rows, all is well. But after I remove the rows in the Unchecked handler, the next time I click on any CheckBox, I get multiple Checked events, one for each time I handled the Unchecked event! The events are not nested so I can't use a flag to block them.
Any ideas? Here's a code snippet for the Unchecked event handler:
// Remove child rows (rows after current row with Level greater than current row)
private void OnUnChecked(object sender, RoutedEventArgs e)
{
e.Handled = true;
DataRow row = ((DataRowView)((CheckBox)sender).DataContext).Row;
List<DataRow> rowsToRemove = new List<DataRow>();
for (int i = table.Rows.IndexOf(row) + 1; i < table.Rows.Count; i++)
{
if ((int)table.Rows[i]["Level"] <= (int)row["Level"])
{
break;
}
rowsToRemove.Add(table.Rows[i]);
}
foreach (DataRow rowToRemove in rowsToRemove)
{
table.Rows.Remove(rowToRemove);
}
}
|