简体   繁体   中英

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.

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>

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM