Search code examples
wpfxamldatagrid.net-3.5wpfdatagrid

DataGridCheckboxColumn two way binding


I am using the DataGrid from the WPF toolkit in .NET 3.5.

I have a datagrid column bound to a boolean property from my source object.

The checkbox is calling the boolean's properties get accessor correctly.

However when checking or unchecking the box the get instead of the set is being called.

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Object,  Source={StaticResource model}, Mode=TwoWay}">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding BoolProperty, mode=TwoWay}"/>                
        </DataGrid.Columns>
</DataGrid>

When I instead use a DataGridTemplateColumn with a Checkbox in it the property is set correctly however then it is more complicated to create a nice layout.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding BoolProperty, Mode=TwoWay}"/>                            
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

What am I doing wrong using the DataGridCheckBoxColumn?


Solution

  • In a DataGrid the bindings do not get committed until you end editing of the row/cell. If you press enter the binding will apply back to the source.

    Using a template like this overrides that behaviour, i wouldn't recommend that though. Also setting TwoWay explicitly should not be necessary.