DanBrum
DanBrum

Reputation: 439

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?

Upvotes: 31

Views: 53543

Answers (4)

Gennadij
Gennadij

Reputation: 71

My solution was to add ElementStyle with Style TargetType="CheckBox":

<DataGridCheckBoxColumn Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}">
          <DataGridCheckBoxColumn.ElementStyle>
                 <Style TargetType="CheckBox"/>
          </DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>

Upvotes: 7

brunnerh
brunnerh

Reputation: 184416

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.

Upvotes: 13

Marek Pavelek
Marek Pavelek

Reputation: 1147

My solution was to set UpdateSourceTrigger to PropertyChanged.

<DataGridCheckBoxColumn Header="Bool property" Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}"></DataGridCheckBoxColumn>

Upvotes: 57

MoonHunter
MoonHunter

Reputation: 611

I got same problem with you ,here is my solution

<CheckBox HorizontalAlignment="Center" IsChecked="{Binding BoolProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Upvotes: 58

Related Questions