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 Marianne,
thank you for the sample.
There seems to be one misunderstanding. After the user has selected rows, columns or specific cells he should be able to toggle the check/uncheck state of all CheckBoxes, within his selection, by pressing just one key - preferably the space key.
I modified my sample application again. Please take a look at the attached project (file WPF_xamGrid_toggleCheckBoxes.zip).
When using the Add key everything works great now and I can toggle the check/uncheck state of all CheckBoxes within the actual selection. This is a familiar behavior for users working with CheckBoxes in a grid. This works for selected rows, columns or specific cells.
The only problem we still have is the fact that all SelectionSettings get lost when you press the Space key!
I implemented a handler "grid_PreviewKeyDown" for the PreviewKeyDown event. After selecting e.g. one column you get the Trace output: grid_PreviewKeyDown e.Key: Space grid.SelectionSettings.SelectedCells.Count: 0 grid.SelectionSettings.SelectedRows.Count: 0 grid.SelectionSettings.SelectedColumns.Count: 1
In the handler "XamGrid_KeyDown" you then get the following output: XamGrid_KeyDown e.Key: Space grid.SelectionSettings.SelectedCells.Count: 1 grid.SelectionSettings.SelectedRows.Count: 0 grid.SelectionSettings.SelectedColumns.Count: 0
Questions:1. Is ist possible to prevent the actual XamGrid behavior after pressing the Space key?2. Is it possible to "cancel" the PreviewKeyDown event, if the Space key was pressed?3. If the previous questions must be answered with "No" could you make a suggestion how we could solve this problem? Do we really have to save the SelectionSetting information in grid_PreviewKeyDown separately for using it afterwards in XamGrid_KeyDown?
Regards,chrisobs
Hi,
I'm attaching my updated version of your sample.
I had apparently left out the selectedcells in my code, which I have added now. The other thing I noticed was that using the Space key with the LeftCtrl key was problematic for selecting cells. So I modified the sample to use the key pad Add button in conjunction with the LeftCtrl key.
You can select cells, rows and columns, using the Shift or Ctrl key in combination with the left mouse button. In order to toggle the checkboxes in the selected cells, rows, and columns, use the combination of Ctrl key plus the Add button to make the checkboxes checked or the Add button alone to make the selected checkboxes unchecked.
Please let me know if you have any questions.
thank you for your reply. I transferred your XamGrid_KeyDown-Code to the test-application, I sent you before, but I still have the problem with the Selection information getting lost. So it is not possible to toggle any checkboxes of former selected cells!
If I select a column and the press the space key I still get the following trace output:HeaderControl_PreviewMouseDown:grid.SelectionSettings.SelectedColumns.Count: 1XamGrid_KeyDown:grid.SelectionSettings.SelectedCells.Count: 1grid.SelectionSettings.SelectedRows.Count: 0grid.SelectionSettings.SelectedColumns.Count: 0
So the main problem remains. At the moment you press the space key, the selections information is lost and only one cell remains selected - probably the former ActiveCell!? How can I solve this problem?
Regards,
chrisobs
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);
Hello,
can anybody of the support team help me on this subject?