仅当单击复选框时才更改 ListViewItem 的选中状态

发布于 2024-08-03 23:49:15 字数 79 浏览 7 评论 0原文

默认情况下,双击 ListViewItem 会切换其选中状态。我只想通过单击项目的复选框或在突出显示项目时按空格键来更改选中状态。这容易做到吗?

By default, double-clicking a ListViewItem toggles its Checked state. I only want the Checked state to be changed by clicking an the item's checkbox or pressing the space bar while an item is highlighted. Is this easy to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2024-08-10 23:49:15

该解决方案涉及 3 个事件和一个 bool 类型的状态变量:

private bool inhibitAutoCheck;

private void listView1_MouseDown(object sender, MouseEventArgs e) {
    inhibitAutoCheck = true;
}

private void listView1_MouseUp(object sender, MouseEventArgs e) {
    inhibitAutoCheck = false;
}

private void listView1_ItemCheck(object sender, ItemCheckEventArgs e) {
    if (inhibitAutoCheck)
        e.NewValue = e.CurrentValue;
}

项目检查可以避免转换到另一检查状态(在 ItemChecked 事件之前调用)。解决方案既简单又确定。

为了找出答案,我用不同的事件做了一个小测试:

单击时:

  1. MouseDown <------------- 禁止区域
  2. 单击
  3. MouseClick
  4. MouseUp ------------- >
  5. ItemCheck(外部禁止区域)
  6. ItemChecked

双击时:

  1. MouseDown <------------- 禁止区域
  2. ItemSelectionChanged
  3. SelectedIndexChanged
  4. Click
  5. MouseClick
  6. MouseUp --------- ---->
  7. MouseDown <------------- 禁止区域
  8. ItemCheck (内部禁止区域)
  9. ItemActivate
  10. DoubleClick
  11. MouseDoubleClick
  12. MouseUp ------------->

The solution involves 3 events and one state variable of type bool:

private bool inhibitAutoCheck;

private void listView1_MouseDown(object sender, MouseEventArgs e) {
    inhibitAutoCheck = true;
}

private void listView1_MouseUp(object sender, MouseEventArgs e) {
    inhibitAutoCheck = false;
}

private void listView1_ItemCheck(object sender, ItemCheckEventArgs e) {
    if (inhibitAutoCheck)
        e.NewValue = e.CurrentValue;
}

The item check enables to avoid the transition to another check state (called before the ItemChecked event). The solution is simple and sure.

To find it out I made a small test with different events:

When clicking:

  1. MouseDown <------------- inhibited region
  2. Click
  3. MouseClick
  4. MouseUp ------------->
  5. ItemCheck (outside inhibited region)
  6. ItemChecked

When double clicking:

  1. MouseDown <------------- inhibited region
  2. ItemSelectionChanged
  3. SelectedIndexChanged
  4. Click
  5. MouseClick
  6. MouseUp ------------->
  7. MouseDown <------------- inhibited region
  8. ItemCheck (inside inhibited region)
  9. ItemActivate
  10. DoubleClick
  11. MouseDoubleClick
  12. MouseUp ------------->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文