0

I have some problem in using DataGridCheckBoxColumn. For some cell, when I check one checkbox, multiple checkboxes are checked at the same time. How should I prevent this? Here is my XAML.

<DataGrid IsReadOnly="True" Margin="10,0,10,8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="2" Name="newsStories" AutoGenerateColumns="False" RowHeaderWidth="0">
    <DataGrid.Columns>
        <DataGridTextColumn Width="8*" Header="Headline" Binding="{Binding Path=heading}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
            <DataGridTextColumn.ElementStyle>
                <Style>
                    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                    <Setter Property="TextBlock.Padding" Value="5" />
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridCheckBoxColumn Header="Select" Width="2*" Binding="{Binding Path=isIncluded, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}">
            <DataGridCheckBoxColumn.HeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridCheckBoxColumn.HeaderStyle>
            <DataGridCheckBoxColumn.ElementStyle>
                <Style>
                    <Setter Property="TextBlock.HorizontalAlignment" Value="Center" />
                    <Setter Property="TextBlock.VerticalAlignment" Value="Center" />
                </Style>
            </DataGridCheckBoxColumn.ElementStyle>
            <DataGridCheckBoxColumn.CellStyle>
                <Style>
                    <EventSetter Event="CheckBox.Checked" Handler="handleChecked" />
                    <EventSetter Event="CheckBox.Unchecked" Handler="handleChecked" />
                </Style>
            </DataGridCheckBoxColumn.CellStyle>
        </DataGridCheckBoxColumn>
    </DataGrid.Columns>
</DataGrid>

I don't have any code-behind to show here. The problem is here even though I don't have any code-behind.

Thank you.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
Cheung Brian
  • 715
  • 4
  • 11
  • 29
  • Can you show the code which is executed in the `CheckBox.Checked` and `CheckBox.Unchecked` events? Where are your setting the `ItemsSource` of the `DataGrid`? – Mighty Badaboom Jul 31 '17 at 10:28
  • there is not context for the handler handleChecked ? what is the code behing? why do you change the data context of the colum ? Binding="{Binding Path=isIncluded – GCamel Jul 31 '17 at 10:40
  • Thanks for the answer. The problem is still here even though I remove the handler. I am developing using VirtualBox with Win7 installed. I think it is the problem. – Cheung Brian Jul 31 '17 at 10:44
  • no, is it all checkboxes or just a few ? what i do not understand in your code is ==> why this binding ? and Where are your setting the ItemsSource of the DataGrid? – GCamel Jul 31 '17 at 11:14
  • It is random. There is a scrollbar in the table. When I check a box, then I scroll down. The checkbox is unchecked when I scroll up. – Cheung Brian Jul 31 '17 at 11:26
  • So basically it's a good old virtualization issue... how about binding `TwoWay` instead of `OneWayToSource`? This would allow the row to re-initialize when scrolling into view again. The fact that the problem appears scrolling-related is essential to the question - you should edit. – grek40 Jul 31 '17 at 12:27

0 Answers0