Dou*_*unk 2 c# data-binding collections struct winforms
您好编码员我还有另一个涉及winforms数据绑定的问题.我设置了一个测试应用程序,其中我有一个由名为CustomerInfo的结构组成的绑定列表.我已将列表框控件绑定到此列表并旋转一个线程以将CustomerInfo项添加到绑定列表.
namespace dataBindingSample {
public partial class Form1 : Form {
public BindingList<CustomerInfo> stringList = new BindingList<CustomerInfo>();
public Thread testThread;
public Form1() {
InitializeComponent();
stringList.AllowNew = true;
stringList.RaiseListChangedEvents = true;
listBox1.DataSource = stringList;
testThread = new Thread(new ThreadStart(hh_net_retask_request_func));
testThread.Priority = ThreadPriority.Normal;
}
private void hh_net_retask_request_func() {
int counter = 1;
while (true) {
CustomerInfo cust = new CustomerInfo();
cust.Name = "Customer "+ counter.ToString();
this.Invoke((MethodInvoker)delegate {
stringList.Add(cust);
});
counter++;
Thread.Sleep(1000);
}
}
private void Form1_Load(object sender, EventArgs e) {
testThread.Start();
}
}
public struct CustomerInfo {
public string Name {
set {
name = value;
}
get {
return name;
}
}
private string name;
}
}
Run Code Online (Sandbox Code Playgroud)
我在列表框中看到的是结构的名称,dataBindingSample.CustomerInfo而不是结构的属性.我的印象是非复杂绑定占据了第一个可用属性.
请告诉我我做错了什么.
谢谢,
您需要向ToString()CustomerInfo类添加一个覆盖,它返回您在列表框中显示的内容,或者在设置listBox1.DisplayMemer = "Name"之前设置DataSource.