对我有用,所以我假设列表中的项目可能存在某些问题(自定义?拦截事件?)或者事件没有正确连接。
例如试试这个(完整的 Form1.cs):
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public class MyObject {
public MyObject(string someValue) {
SomeValue = someValue;
}
protected string SomeValue { get; set; }
public override string ToString() {
return SomeValue;
}
}
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var list = new List<MyObject> {
new MyObject("Item one"), new MyObject("Item two")
};
listBox1.DataSource = list;
}
private void listBox1_DoubleClick(object sender, EventArgs e) {
Debug.WriteLine("DoubleClick event fired on ListBox");
}
}
}
使用包含以下内容的设计器源文件 (Form1.Designer.cs):
private void InitializeComponent() {
this.listBox1 = new System.Windows.Forms.ListBox();
... // left out for brevity
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
作为测试,通过模板创建一个新的 Forms 基础应用程序,然后只添加 ListBox 并定义一个 MyObject 类。查看您是否观察到相同或不同的行为。