代码之家  ›  专栏  ›  技术社区  ›  Kevin.Yang

有没有办法在ObjectListView中不按Ctrl键进行多选?

  •  2
  • Kevin.Yang  · 技术社区  · 7 年前

    在ObjectListView组件中,我将FullRowSelect和MultiSelect属性设置为true,但仍然需要按Ctrl键来选择多行。那么,有没有一种方法可以在不按下Ctrl或shift键的情况下进行多选?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Rev    7 年前

    我想我有一个可行的解决方案。本例中的模型对象类型为“Item”。有关一些解释,请参阅代码注释。

    // We require a list to remember which items should stay selected
    private List<Item> _MultiSelectList;
    
    public Form1() {
        // <other stuff>
        _MultiSelectList = new List<Item>();
    }
    
    // use this event to check which item has been clicked
    private void objectListView1_MouseClick(object sender, MouseEventArgs e) {
        objectListView1.BeginUpdate();
    
        // any item clicked?
        if (objectListView1.MouseMoveHitTest.Item != null) {
            var item = objectListView1.MouseMoveHitTest.Item.RowObject as Item;
    
            // model object of expected type available?
            if (item != null) {
                // add or remove item from list to effectively toggle selection
                if (_MultiSelectList.Contains(item)) {
                    _MultiSelectList.Remove(item);
                } else {
                    _MultiSelectList.Add(item);
                }
            }
        }
    
        // select all desired items
        objectListView1.SelectObjects(_MultiSelectList);
        objectListView1.EndUpdate();
    }
    
    // optional: to prevent flickering from the native item selection change, we freeze the OLV contents during the mouse click
    private void objectListView1_MouseDown(object sender, MouseEventArgs e) {
        objectListView1.Freeze();
    }
    
    private void objectListView1_MouseUp(object sender, MouseEventArgs e) {
        objectListView1.Unfreeze();
    }