有没有办法在不按下 Ctrl 键的情况下在 ObjectListView 中进行多选?
Is there a way to do multiselect in ObjectListView without the Ctrl key pressed?
我在 ObjectListView 组件中将 FullRowSelect 和 MultiSelect 属性 设置为 true,但我仍然需要通过按 Ctrl 键来 select 多行。那么,有没有办法在不按下 Ctrl 或 shift 键的情况下执行 multiselect?
我想我有一个可行的解决方案。此示例中的模型对象的类型为 "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();
}
我在 ObjectListView 组件中将 FullRowSelect 和 MultiSelect 属性 设置为 true,但我仍然需要通过按 Ctrl 键来 select 多行。那么,有没有办法在不按下 Ctrl 或 shift 键的情况下执行 multiselect?
我想我有一个可行的解决方案。此示例中的模型对象的类型为 "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();
}