12
votes

I have this tiny little issue with the datagrid.

In my grid I have a checkbox column which is the only editable column.

The behavior that I'm looking for is for the datagrid to update i's datasource as soon as the status of the checkbox changes. So user checks/unchecks the box > underlying datatable gets updated.

The default behavior seems to update the source when the row loses focus requiring the user to press a key or click on some other control to save the changes.
How can I change this behavior?

I don't see any property for the datagrid that could do this and no CheckChanged event for DataGridCheckBoxColumn.

2

2 Answers

6
votes

The DataGrid itself sets the UpdateSourceTrigger for all columns (aside from template columns) to be LostFocus and this can't be overridden. Hence the need to use template columns with a checkbox template.

EDIT: This is just one in a long list of silly gotchas around DataGrid columns. More are outlined here.

18
votes

You need the UpdateSourceTrigger property on the binding of the column. Here is a quick example, you can flesh it out and fill in the blanks:

<DataGrid x:Name="someGrid">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Binding="{Binding SomeProperty, UpdateSourceTrigger=PropertyChanged}" />
    </DataGrid.Columns>
</DataGrid>