c# - Winforms 涉及结构体的绑定(bind)问题

标签 c# winforms data-binding collections struct

嗨,编码员,我还有另一个涉及 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/

相关文章:

c# - 简单的 Windows 窗体数据绑定(bind)

Android DataBinding 3.0.1 对可观察变量的调用方法导致生成导致 NPE 的错误代码

c# - 演示受限执行区域重要性的代码

c# - 在 KeyUp 事件中检测按键

c# - ObservableCollection<T> 不更新 ListBox

c# - 工具提示文本离开屏幕(WinForms)

c# - 获取导致异常的表单名称、方法名称和更多详细信息

c# - 在 C# 中打印控件的内容?

c# - 为什么返回 ` ActionResult<IEnumerable<ZapResult>>` 的 ASP.NET Core 操作可以返回任何对象的 `BadRequest()`?

c# - Azure Fabric 服务中的启动任务