Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF datagrid - commit changes in a checkbox column as soon as value is changed

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.

like image 329
Steinthor.palsson Avatar asked Jun 06 '11 01:06

Steinthor.palsson


2 Answers

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>
like image 62
slugster Avatar answered Sep 28 '22 03:09

slugster


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.

like image 29
Justin Simon Avatar answered Sep 28 '22 02:09

Justin Simon