本文介绍了的WinForms绑定到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体有一个列表框。窗体有这个方法

I have a Windows Form with a ListBox. The form has this method

public void SetBinding(BindingList<string> _messages)
{
  BindingList<string> toBind = new BindingList<string>( _messages );
  lbMessages.DataSource = toBind;
}

在其他地方,我有一个类叫做管理器具有这种特性

Elsewhere, I have a class called Manager that has this property

public BindingList<string> Messages { get; private set; }

和这条线在其构造

Messages = new BindingList<string>();

最后,我有一个实例的形式和经理我的启动程序,然后调用

Finally, I have my startup program that instantiates the form and the manager and then calls

form.SetBinding(manager.Messages);

还有什么我必须这样做,像这样在管理器语句:

What else do I have to do so that a statement in Manager like this:

Messages.Add("blah blah blah...");

将导致加入到线和在表单的列表框立即显示?

will cause a line to be added to and displayed immediately in the form's ListBox?

我根本就不要做这种方式。我只是希望我的经理类能够发布到形式,而它做的工作。

I don't at all have to do it this way. I just want my Manager class to be able to post to the form while it is doing its job.

推荐答案

我认为这个问题是与你的 SetBinding 方法,你在哪里制作的绑定列表,这意味着你是不是绑定到该管理器列表对象了。

I think the problem is with your SetBinding method where you are making a new binding list, which means you aren't binding to the list in the Manager object anymore.

尽量只传递当前的BindingList到数据源:

Try just passing the current BindingList to the datasource:

public void SetBinding(BindingList<string> messages)
{
  // BindingList<string> toBind = new BindingList<string>(messages);
  lbMessages.DataSource = messages;
}

这篇关于的WinForms绑定到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:12