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.
Hello Marianne,
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?
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.
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
Glad we were able to resolve your question.
Please let me know if there is anything more I can help you with.
this was the hint I was waiting for.
I moved my code from the KeyDown to my PreviewKeyDown method and now everything works fine.
You could use the XamGrid's PreviewKeyDown, check if it's a Space key and then set e.Handled to true. That will stop any processing that would have been generated.
You could use code like this.
e.Handled = true;
Let me know if that helps you.
thank you for the reply.
I already know the behavior of XamGrid, when pressing the space key. My question was how to prevent XamGrid from doing that.According to your answer it seems to be impossible to realize that.
In my last sample I sent you, I already realized toggling the CheckBoxes according to their current state, so I don't know why you worked on that issue.We want to realize the toggling as I have implemented it. Based on the value of the first cell of the selected columns, rows or cells we toggle all other cells.
You wrote "you don't need the code in your PreviewMouseMove and PreviewMouseDown", but we do need this code. Otherwise you can't select multiple columns, just by selecting one column and moving with the mouse over other columns, keeping the left mouse button pressed.
So up to now I have no further questions.
Rather than cancel the native behavior associated with the spacebar, I would recommend that you use another key. The space bar clears the selected collections and leaves the last cell that had focus, selected. Run the sample and hit the spacebar as your first step. See what happens. Then click below the rows in the empty area and notice that the first cell in the first row has focus. Then hit the spacebar again and see that that cell is selected.
If you want to use the Add key alone, you could use it to toggle the value in the checkboxes.
I modified the sample, adding code to toggle the value from false to true based on the current value. You'll also see code to determine if the SelectedColumns collection is resetting a cell that would have already been set from the SelectedRows collection.
I'll attach my sample for you to review.
You have RowSelection and ColumnSelection and CellSelection set to Multiple so you don't need the code in your PreviewMouseMove and PreviewMouseDown. But you do need to use the SelectedColumns, SelectedRows and SelectedCells collections from a key event and check for the specific key you want to use.
Please let me know if you have further questions.