简体   繁体   English

鼠标滚动检查并取消选中DataGrid中的复选框

[英]Mouse Scroll check and uncheck checkboxes in a DataGrid

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. 我正面临着这个问题,我在Datagrid中绑定了复选框,当我们单击复选框时我会进行一些简单的数学计算,它应该在文本框中添加总和而它正在这样做,但问题是鼠标向上滚动并自动向下检查和取消选中复选框现在文本框中所选项目的总价格更高,选中的复选框更少或有时更多,因此鼠标向上或向下滚动会产生此问题。 Any idea???? 任何的想法???? thanks 谢谢

问题预览

I had exactly the same problem, with the following scenario : 我有完全相同的问题,具有以下场景:

  • You have a DataGrid, each row contains a checkbox 你有一个DataGrid,每行包含一个复选框
  • The checkboxes have their Checked/Unchecked events set : if the selection is multiple, all the checkboxes are toggled 复选框设置了Checked / Unchecked事件:如果选择为多个,则会切换所有复选框
  • 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 只要它们从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. Jacek提供的解决方案有效,但内容不再使用鼠标滚轮滚动,鼠标光标必须位于滚动条上。

What seems to work in my project is to add this property to the DataGrid 在我的项目中似乎有用的是将此属性添加到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. 这很奇怪,因为它告诉Datagrid不可滚动,但是,内容仍然可以通过鼠标滚轮滚动,并且不再发生切换复选框状态的问题。

您需要在网格中设置VirtualizingStackPanel.IsVirtualizing="False"

I had the same problem in Silverlight. 我在Silverlight中遇到了同样的问题。 When You scrolling, DataGrid fires RowLoading event, and re-initializes the CheckBoxes. 滚动时,DataGrid会触发RowLoading事件,并重新初始化CheckBoxes。 I found only one way to resolve it, don't use builtin vertical scroll bar DataGrid, insted set DataGrid height to auto and place it in ScrollViewer. 我发现只有一种方法可以解决它,不要使用内置的垂直滚动条DataGrid,将DataGrid的高度设置为auto并将其放在ScrollViewer中。

<ScrollViewer VerticalScrollBarVisibility="Visible">
   <DataGrid Height="Auto" />
</ScrollViewer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM