如何保持选中的项目? - 列表视图

发布于 2024-08-26 18:46:02 字数 154 浏览 17 评论 0原文

当用户单击没有项目的空间时,我想在 ListView 上保持选中的项目。例如,项目下方的空间,但仍在 ListView 组件上。我已将 ListView 属性“HideSelection”更改为 false,但这仅在焦点更改为另一个组件时才有效;当用户单击 ListView 本身时不会。谢谢!

I would like to keep an item selected, on a ListView, when the user clicks on a space which has no item. For example, the space below the items, but still on the ListView component. I've change the ListView property "HideSelection" to false, but that only works when the focus is changed to another component; not when the user clicks on the ListView itself. Thanks!

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

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

发布评论

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

评论(2

趴在窗边数星星i 2024-09-02 18:46:02

这是您通常不应该修复的问题。用户有意点击某处,这很可能是因为她想要取消选择某个项目。如果这是无意的,那么她会明白发生了什么并知道如何纠正。为标准控件提供非标准行为往往只会让用户感到困惑。

但你可以修复它。您需要防止本机 ListView 控件看到单击。这需要重写 WndProc() 方法并检查单击发生的位置。将新类添加到您的项目中并粘贴下面所示的代码。编译。将新控件从工具箱顶部拖放到窗体上。

using System;
using System.Drawing;
using System.Windows.Forms;

class MyListView : ListView {
  protected override void WndProc(ref Message m) {
    if (m.Msg == 0x201 || m.Msg == 0x203) {  // Trap WM_LBUTTONDOWN + double click
      var pos = new Point(m.LParam.ToInt32());
      var loc = this.HitTest(pos);
      switch (loc.Location) {
        case ListViewHitTestLocations.None:
        case ListViewHitTestLocations.AboveClientArea:
        case ListViewHitTestLocations.BelowClientArea:
        case ListViewHitTestLocations.LeftOfClientArea:
        case ListViewHitTestLocations.RightOfClientArea:
          return;  // Don't let the native control see it
      }
    }
    base.WndProc(ref m);
  }
}

This is something you normally shouldn't fix. The user clicked somewhere intentionally, that might well be because she wanted to deselect an item. If it was unintentional then she'll understand what happened and know how to correct it. Giving standard controls non-standard behavior tends to only confuse the user.

But you can fix it. You'll need to prevent the native ListView control from seeing the click. That requires overriding the WndProc() method and checking where the click occurred. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto the form.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyListView : ListView {
  protected override void WndProc(ref Message m) {
    if (m.Msg == 0x201 || m.Msg == 0x203) {  // Trap WM_LBUTTONDOWN + double click
      var pos = new Point(m.LParam.ToInt32());
      var loc = this.HitTest(pos);
      switch (loc.Location) {
        case ListViewHitTestLocations.None:
        case ListViewHitTestLocations.AboveClientArea:
        case ListViewHitTestLocations.BelowClientArea:
        case ListViewHitTestLocations.LeftOfClientArea:
        case ListViewHitTestLocations.RightOfClientArea:
          return;  // Don't let the native control see it
      }
    }
    base.WndProc(ref m);
  }
}
茶花眉 2024-09-02 18:46:02

一种方法:在 SelectedIndexChanged 事件上,检查该值是否为 -1;如果是这样,请将其重置为以前的值(您可能将其存储在变量中......)

One way: on the SelectedIndexChanged event, check to see if the value is -1; if so, reset it to the previous value (which you maybe stored in a variable...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文