1

There is a WPF DataGrid with columns that have checkboxes. These checkboxes are already bound to some data source. What we want is a checkbox for each column that checks/unchecks all the checkboxes (and as a result updates the data source so that all the values are the same).

Existing solutions on here work if there isn't an existing datasource - but we have effectively two, the 'select all' checkboxes and the actual data that the other checkboxes in the rows are bound to, if that makes sense.

If we could use a trigger or somesuch instead of 'code behind' that would be ideal. Is there a declarative solution?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fugu
  • 361
  • 2
  • 6
  • 17
  • What is the datasource for the datagrid? Is it using a binding such as ItemsSource="{Binding MyItemsCollection}"? – Adrian Jun 08 '11 at 16:26
  • The data source is actually the collection of columns from another DataGrid. So it uses ItemsSource="{Binding Element=... }" – Fugu Jun 08 '11 at 16:28

2 Answers2

2

Make your CheckBox's Click event point to a Command in your ViewModel that iterates your DataSource and sets IsChecked to true.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Thankyou but that won't work, this is my fault for not being explicit enough. The DataSource is in the View (another DataGrid) rather than the ViewModel. This is entirely UI interaction logic (using checkboxes to freeze columns, etc.) so it doesn't the ViewModel. – Fugu Jun 08 '11 at 16:31
  • @Fugu Couldn't you still use the code-behind and get your DataSource for the DataGrid, then iterate through that and set `IsChecked=true`? – Rachel Jun 08 '11 at 16:41
  • Thanks Rachel, this was how we did it in the end. It's a shame there doesn't seem to be a way to do it without accessing the underlying datasource :) – Fugu Jun 09 '11 at 09:09
  • @Rachel, can I bind the checkbox Click event to a button click event? if so how? –  May 02 '17 at 16:47
  • @NewCoderInTown I suppose, but OnClick is usually something in the View Layer, so it's not a problem that they both have the same reference. I'd suggest making a new question about exactly what you're looking for. You can bind Events (such as OnClick) to commands, or you can create a Button (so you can bind the Command), and draw it using a CheckBox. – Rachel May 02 '17 at 21:02
-1

The standard solutions can work with a databound grid. An example is found (http://www.4guysfromrolla.com/articles/120810-1.aspx). This example if fully client side.

The problem comes when you postback, but that is only a problem if you are not persisting the check all back to the database and refreshing the data you are binding. In those cases, you need to ensure something is passed back to indicate the check all so you can dynamically set the boxes again after a postback.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32