Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse Scroll check and uncheck checkboxes in a DataGrid

Tags:

c#

wpf

private void OnChecked(object sender, RoutedEventArgs e)
{
    try
    {
        LAB_TEST t = new LAB_TEST();
        CheckBox chk = (CheckBox)e.OriginalSource;
        if (e.OriginalSource is CheckBox)
        {
            int OID = Convert.ToInt32(((CheckBox)chk).Tag);
            t = eb.TestGetByOID(OID);
            bool has = advisedTests.Any(test => test.OID == OID);
            if (!has)
            {
                if (txtGrossAmount.Text != string.Empty)
                {
                    decimal amount = Convert.ToDecimal(txtGrossAmount.Text);
                    amount += Convert.ToDecimal(t.PRICE);
                    txtGrossAmount.Text = amount.ToString();
                }
                else
                {
                    txtGrossAmount.Text = t.PRICE.ToString();
                }
                advisedTests.Add(t);
            }
        }
    }
    catch (Exception ex)
    {

    }
}

I'm facing this problem that I've bound checkboxes in Datagrid and I'm making some simple math calculations when we click the check box it should add the sum in the textbox and it is doing this but the problem is that mouse scrolling up and down check and uncheck checkboxes automatically now the total price of the selected items in the textbox is more and selected checkboxes are less or sometimes more so mouse scrolling up or down creating this problem. Any idea???? thanks

Issue Preview

like image 908
dnxit Avatar asked Dec 20 '11 15:12

dnxit


2 Answers

You need set VirtualizingStackPanel.IsVirtualizing="False" in your Grid!

like image 191
Sponsor Avatar answered Nov 04 '22 08:11

Sponsor


I had exactly the same problem, with the following scenario :

  • You have a DataGrid, each row contains a checkbox
  • The checkboxes have their Checked/Unchecked events set : if the selection is multiple, all the checkboxes are toggled
  • Select multiple rows
  • Check one checkbox, the whole selection is checked
  • Keep the selected rows, and scroll to make them invisible
  • The seleted rows checkboxes should be checked/unchecked as soon as they disappear from the UI

The solution given by Jacek works, but the content is not scrollable with mouse wheel anymore, the mouse cursor has to be on the scrollbar.

What seems to work in my project is to add this property to the DataGrid

ScrollViewer.CanContentScroll="False" 

It's very weird, as it tells the Datagrid not to be scrollable, BUT, the content is still scrollable via the mouse wheel, and the issue of toggling the checkboxes status doesn't happen anymore.

like image 36
MrPingouin Avatar answered Nov 04 '22 08:11

MrPingouin