samedi 23 juillet 2016

Automatic checking and unchecking of checkboxes in WPF DataGrid on mouse scroll

I am developing a WPF application in which I need to send SMS to only those contacts that are checked in a DataGrid.

After checking the desired contacts in the DataGrid, I am facing problems when I scroll down/up. The checked property of the checkboxes in the DataGrid gets changed randomly.

I came across some solutions that suggested to add the following properties:

  • VirtualizingStackPanel.VirtualizationMode="Standard" in DataGrid
  • VirtualizingStackPanel.IsVirtualizing="True" in DataGrid (When I set it to False, the application becomes totally unresponsive)
  • UpdateSourceTrigger="PropertyChanged" in CheckBox Binding
  • EnableRowVirtualization="True" in DataGrid
  • EnableColumnVirtualization="True" in DataGrid

I tried them all, but none of them worked.

XAML:

<StackPanel Orientation="Horizontal">
        <DataGrid x:Name="smsgrid" VirtualizingStackPanel.VirtualizationMode="Standard" VirtualizingStackPanel.IsVirtualizing="True" Margin="10,20,0,10" AutoGenerateColumns="False" IsReadOnly="True" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="chkbox" Checked="chkbox_Checked" Unchecked="chkbox_Checked"></CheckBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="ID" Binding="{Binding ID}"></DataGridTextColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
                <DataGridTextColumn Header="Mobile no." Binding="{Binding Mobile1}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        ...
<StackPanel>

I am using the StackPanel to stack a LogIn page horizontally (not pasted in the above XAML) and a List to store the mobile number if the corresponding checkbox is selected. I also noted that the values in the List remains unchanged as the values of the checked property of checkboxes changes.

It would be really nice if I get a solution for avoiding the automatic checking and unchecking of checkboxes in the DataGrid.

Thank You!




Aucun commentaire:

Enregistrer un commentaire