嗨,编码员,我还有另一个涉及 winforms 中数据绑定(bind)的问题。我设置了一个测试应用程序,其中有一个由名为 CustomerInfo 的结构组成的绑定(bind)列表。我已将一个列表框控件绑定(bind)到此列表,并旋转一个线程以将 CustomerInfo 项添加到绑定(bind)列表。
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;
}
}
我在列表框中看到的是结构体 dataBindingSample.CustomerInfo
的名称,而不是该结构体的属性。我的印象是非复杂绑定(bind)占据了第一个可用的属性。
请告诉我我做错了什么。
谢谢
最佳答案
您需要向 CustomerInfo 类添加对 ToString()
的重写,以返回您希望在列表框中显示的内容,或者设置 listBox1.DisplayMemer = "设置
。数据源
之前的名称”
关于c# - Winforms 涉及结构体的绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6379468/