DataGridCheckboxColumn two way binding
Asked Answered
G

4

30

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?

Gaylagayle answered 18/5, 2012 at 10:46 Comment(0)
H
12

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.

Hangeron answered 18/5, 2012 at 10:50 Comment(2)
Ok thanks in this case the behaviour is necessary as when the user checks the box I want to visually update something on the screen. Is there are way to do this using the DatGridCheckBoxColumn? Also what is the best way so that on the inital click the user doesn't have to click twice to select the row then the checkbox, can you select the row with the mouseover event?Gaylagayle
@DanBrum: I don't know if you can circumvent this using that column, you could define your own DataGridCheckBoxColumn if you don't find a way. Selection on MouseOver should be possible.Hangeron
T
58

I got same problem with you ,here is my solution

<CheckBox HorizontalAlignment="Center" IsChecked="{Binding BoolProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Tirzah answered 21/3, 2013 at 14:24 Comment(3)
Thank you sir, I had to search the web for a while before finding this solution. Works great.Emerald
TwoWay is not necessary as it's default.Orvieto
Interesting, in WinUI3, only this way works. I tried below method to bind, did not work. <DataGridCheckBoxColumn Header="Bool property" Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}"></DataGridCheckBoxColumn>Jaramillo
C
55

My solution was to set UpdateSourceTrigger to PropertyChanged.

<DataGridCheckBoxColumn Header="Bool property" Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}"></DataGridCheckBoxColumn>
Cool answered 17/10, 2014 at 7:0 Comment(2)
This is the best answer.Coacervate
this should be the answerMicroprint
H
12

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.

Hangeron answered 18/5, 2012 at 10:50 Comment(2)
Ok thanks in this case the behaviour is necessary as when the user checks the box I want to visually update something on the screen. Is there are way to do this using the DatGridCheckBoxColumn? Also what is the best way so that on the inital click the user doesn't have to click twice to select the row then the checkbox, can you select the row with the mouseover event?Gaylagayle
@DanBrum: I don't know if you can circumvent this using that column, you could define your own DataGridCheckBoxColumn if you don't find a way. Selection on MouseOver should be possible.Hangeron
H
7

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

<DataGridCheckBoxColumn Binding="{Binding BoolProperty, UpdateSourceTrigger=PropertyChanged}">
          <DataGridCheckBoxColumn.ElementStyle>
                 <Style TargetType="CheckBox"/>
          </DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
Hardnosed answered 29/7, 2016 at 9:56 Comment(1)
This is the best answer. Thanks broHutt

© 2022 - 2024 — McMap. All rights reserved.