【问题标题】:How to keep other items selected in System.Windows.Forms.ListView when deselecting one item取消选择一项时如何保持在 System.Windows.Forms.ListView 中选择的其他项目
【发布时间】:2018-08-01 01:21:17
【问题描述】:

当您在ListView 中选择了多个项目并单击其中一个项目时,默认行为是取消选择所有其他项目,只留下被单击的项目。我想要完全相反的行为:单击一个选定的项目会取消选择该项目并保留其他项目的选中状态。

我见过像this onethis one 这样的答案。第一个是关于防止鼠标点击做任何我不想要的事情,第二个是关于取消索引更改事件。我尝试根据我的需要调整后者,但它仍然导致其他项目被取消选择。

private void HandleIncludableFilesListViewSelectedIndexChanging
    (object sender, Controls.Events.ListViewItemChangingEventArgs e)
{
   if (_includableFilesListView.Items[e.Index].Selected) e.Cancel = true;
}

上述事件处理程序仅针对被单击的单个项目触发,而不是针对所有其他被取消选择的项目。

有什么方法可以实现吗?

【问题讨论】:

    标签: c# .net winforms listview multipleselection


    【解决方案1】:

    作为一个选项,您可以覆盖DefWndProc 并处理WM_LBUTTONDOWN。然后做hit-test并检查点击的点是否是一个项目,恢复项目的Selected属性:

    public class MyListView : ListView
    {
        const int WM_LBUTTONDOWN = 0x0201;
        protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == WM_LBUTTONDOWN)
            {
                int x = (m.LParam.ToInt32() & 0xffff);
                int y = (m.LParam.ToInt32() >> 16) & 0xffff;
                var item = this.HitTest(x, y).Item;
                if (item != null)
                    item.Selected = !item.Selected;
                else
                    base.DefWndProc(ref m);
            }
            else
            {
                base.DefWndProc(ref m);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2020-05-06
      • 2017-08-13
      • 2021-06-21
      • 1970-01-01
      • 2011-10-24
      • 2018-06-29
      相关资源
      最近更新 更多