1

我想在ListView中实现一些Space键的自定义行为。基本上我想切换光标下项目的选定状态 - 这应该相当简单

this.FocusedItem.Selected = !this.FocusedItem.Selected;

但是,它也执行默认操作,即选择焦点项目。这样我就无法“取消选择”重点项目。我一直在寻找类似的问题,他们建议使用PreviewKeyDown事件,我将在其中处理密钥并禁止 ListView 执行其默认操作。但是 PreviewKeyDown 事件参数没有“处理”属性,所以我不能“吃”这个键。

4

1 回答 1

1

这如你所愿:

private void listView1_KeyDown(object sender, KeyEventArgs e) {
  if (e.KeyData == Keys.Space) {
    listView1.FocusedItem.Selected = !listView1.FocusedItem.Selected;
    e.Handled = e.SuppressKeyPress = true;
  }
}
于 2010-03-31T09:56:24.043 回答