Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF binding for DataGridCheckBoxColumn not updating

Tags:

c#

wpf

I am trying to figure out how to correctly handle the DataGridCheckBoxColumn. I have a list of data that includes a 'Publish' option. When checked the object is flagged to be published to a different server. So far here is what I have:

<DataGrid x:Name="grdEducationalPopups" 
              HorizontalAlignment="Left" VerticalAlignment="Top"
              Margin="19,155,0,0"                   
              AlternationCount="2"
              AutoGenerateColumns="False"
              ItemsSource="{Binding PopupCollection}"
              Loaded="grdEducationalPopups_Loaded"
              MinRowHeight="26"
              RowDetailsTemplate="{StaticResource RowDetailTemplate}" RowDetailsVisibilityChanged="grdEducationalPopups_RowDetailsVisibilityChanged" SelectionChanged="grdEducationalPopups_SelectionChanged" Grid.ColumnSpan="2">           
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <ToggleButton x:Name="RowHeaderToggleButton" Click="ToggleButton_Click" Cursor="Hand"/>                    
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>
        <DataGrid.Columns>                
            <DataGridTextColumn Width="150" Binding="{Binding DisplayName}" Header="Form Name" IsReadOnly="True"/>
            <DataGridTextColumn Width="280" Binding="{Binding URLLocation}" Header="Page Address"/>
            <DataGridTextColumn Width="125" Binding="{Binding DateLastTouched}" Header="Date Modified" IsReadOnly="True"/>
            <DataGridTextColumn Width="125" Binding="{Binding DateRowAdded}" Header="Date Added" IsReadOnly="True"/>
            <DataGridCheckBoxColumn Header="Publish" Binding="{Binding Path=Publish}">
                <DataGridCheckBoxColumn.CellStyle>
                    <Style>
                        <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
                    </Style>
                </DataGridCheckBoxColumn.CellStyle>
            </DataGridCheckBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

I do have OnClick code but it is not doing anything right now except for allowing me to check the values. The value within my list for publish does not change. The publish value for the given record remains false when it should be true. If I change the default value from false to true and launch my application, then all checkboxes are checked, which is what I would expect. This tells me that my binding is correct.

How do I correctly manage user changes so that I can act on the changes appropriately? In my code behind I get a DataGridCell object back and can determine the row data from there, and I suppose edit that data. But I was under the impression that Binding the data should handle this.

like image 765
Daniel Lee Avatar asked Aug 31 '15 12:08

Daniel Lee


1 Answers

There is nothing wrong with your code. It's just that the value is not immediately updated, only after the ComboBox loses its focus.

You can change that by setting UpdateSourceTrigger=PropertyChanged on the binding.

<DataGridCheckBoxColumn Header="Publish" Binding="{Binding Path=Publish, UpdateSourceTrigger=PropertyChanged}">
like image 161
Domysee Avatar answered Nov 17 '22 07:11

Domysee