Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 561
3 revs, 2 users 93% Avatar asked May 18 '12 10:05

3 revs, 2 users 93%


3 Answers

I got same problem with you ,here is my solution

<CheckBox HorizontalAlignment="Center" IsChecked="{Binding BoolProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
like image 93
MoonHunter Avatar answered Oct 23 '22 12:10

MoonHunter


My solution was to set UpdateSourceTrigger to PropertyChanged.

<DataGridCheckBoxColumn Header="Bool property" Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}"></DataGridCheckBoxColumn>
like image 33
Marek Pavelek Avatar answered Oct 23 '22 11:10

Marek Pavelek


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.

like image 11
H.B. Avatar answered Oct 23 '22 12:10

H.B.