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.
Glad we were able to resolve your question.
Please let me know if there is anything more I can help you with.
Hello Marianne,
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.
Regards,chrisobs
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.