2011-06-16 81 views
2

您好编码器我还有另一个涉及winforms数据绑定的问题。我建立了一个测试应用程序,其中有一个由名为CustomerInfo的结构组成的绑定列表。我已经将列表框控件绑定到此列表,并旋转一个线程以将CustomerInfo项添加到绑定列表。Winforms涉及结构的绑定问题

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相对于该结构的属性的名称。我的印象是,非复杂的约束取得了第一个可用的财产。

请教我关于我做错了什么。

感谢,

回答

5

你要么需要添加的ToString()倍率时,返回你会在你的列表框中喜欢持续显示,或设置DataSource之前设置listBox1.DisplayMemer = "Name"你CustomerInfo类。

+0

令人惊叹。非常感谢,我接受了这个答案。还有一件事,你有什么好的winform数据绑定书建议?我从2006年发现了一些,但并不像我喜欢的那么近。 – DoubleDunk 2011-06-16 22:51:19

+0

@DoubleDunk - 没问题,很乐意提供帮助。我对书籍没有任何建议,恐怕 - 您应该可以在这里找到一些建议,也可以在程序员.stackexchange.com上找到一些建议。 – 2011-06-16 22:57:18

+0

真的不会有任何书籍,Wpf是新开发的首选。尽管如此,WinForms数据绑定并没有真正改变太多,我怀疑它不会很快。 – Andy 2011-06-17 01:26:11