Hello,in our application we have a grid with CheckBoxColumns and the user should be able to easily check/uncheck multiple checkboxes at once.Therefore it must be possible to select multiple rows, columns or ranges of cells to check/uncheck the containing checkboxes by using the Space key.To make it easy for you I modified a sample from you, which was created by Stefan (MCPD) on 09-09-2013 9:43 AM for a post with the topic "XamGrid - multiple column selection through column header".I added a method to create the grid layout, some Trace output and a KeyDown handler.The grid you will see only contains CheckBoxColumns and it is possible to select multiple rows, columns or cells. Please take a look at the PNG files in the attached zip file (XamGrid How to toggle multiple CheckBoxes.zip).Problem:The selection of multiple rows, columns or cells works fine, but when I then press the space key and my KeyDown handler is called only one cell is still selected.Example:If you select 3 columns you get the following trace output:HeaderControl_PreviewMouseDown: grid.SelectionSettings.SelectedColumns.Count: 1HeaderControl_PreviewMouseMove: grid.SelectionSettings.SelectedColumns.Count: 2HeaderControl_PreviewMouseMove: grid.SelectionSettings.SelectedColumns.Count: 3If you then press the space key you get the following trace output:XamGrid_KeyDown: grid.SelectionSettings.SelectedCells.Count: 1 grid.SelectionSettings.SelectedRows.Count: 0 grid.SelectionSettings.SelectedColumns.Count: 0 This behavior has nothing to do with the CheckBox columns, because I got the same trace output with the original sample from you which had no CheckBox columns.Questions:1. How can I get the selection information I need to change the check/uncheck state of the selected checkboxes?2. What would be the best way to change the check/uncheck state of multiple checkboxes in code behind?Regards,chrisobs
Hello,
can anybody of the support team help me on this subject?
Regards,
chrisobs
Hi,
Please excuse the delay in responding.
Your code in the KeyDown event needs to identify whether there are any SelectedCells, SelectedRows and SelectedColumns and then update the underlying data which will be reflected in the checkbox.
I ended up having to also use an additional key to determine if the value should be set to true or false.
I think the following code will achieve what you want.
private void XamGrid_KeyDown(object sender, KeyEventArgs e)
{
DataView dv = grid.ItemsSource as DataView;
DataTable d = dv.Table as DataTable;
if (e.Key == Key.Space)
System.Diagnostics.Trace.WriteLine("XamGrid_KeyDown:");
System.Diagnostics.Trace.WriteLine(" grid.SelectionSettings.SelectedCells.Count: " + grid.SelectionSettings.SelectedCells.Count.ToString());
System.Diagnostics.Trace.WriteLine(" grid.SelectionSettings.SelectedRows.Count: " + grid.SelectionSettings.SelectedRows.Count.ToString());
System.Diagnostics.Trace.WriteLine(" grid.SelectionSettings.SelectedColumns.Count: " + grid.SelectionSettings.SelectedColumns.Count.ToString());
//Requirement for our Application: Toggle Check/Uncheck state of all CheckBoxes in the secected columns or rows
for (int r = 0; r < grid.SelectionSettings.SelectedRows.Count; r++)
int ri = grid.SelectionSettings.SelectedRows[r].Index;
for (int c = 0; c < d.Rows[ri].ItemArray.AsEnumerable().Count(); c++)
if (!Keyboard.IsKeyDown(Key.LeftCtrl))
d.Rows[ri].SetField<bool>(c, false);
}
else
d.Rows[ri].SetField<bool>(c, true);
for (int c = 0; c < grid.SelectionSettings.SelectedColumns.Count; c++)
string ckey = grid.SelectionSettings.SelectedColumns[c].Key;
for (int r = 0; r < d.Rows.Count; r++)
d.Rows[r].SetField<bool>(ckey, false);
d.Rows[r].SetField<bool>(ckey, true);
Please let me know if you have any questions.