Bind List to WinForms-Listbox

2019-08-09 05:46发布

问题:

I have an small (probably dumb) issue with databinding. I try to bind a List

List<double> _measuredValues = new List<double>();

to a winforms ListBox.

In Form_Load I set:

lstMeasuredValues.DataSource = _measuredValues;

When I update the values, nothing appears?!

_measuredValues.Add(numBuffer);

One thing I thought about is a data type issue. But how do I change the type just to change it into a string?

lstMeasuredValues.DataSource = _measuredValues.ToString().ToList();

Another reason might be that the upper line of code is within another thread. But I think this should not be the problem.

How can I bind this list?

回答1:

When I update the values, nothing appears?!

_measuredValues.Add(numBuffer);

In order to allow UI to reflect the data source modifications, the data source must provide some sort of a change notification. WinForms list data binding infrastructure uses ListChanged event of the IBindingList Interface. There is a standard provided BindingList<T> class which can be used instead of List<T> to get the desired behavior. All you need is changing this line

List<double> _measuredValues = new List<double>();

to

BindingList<double> _measuredValues = new BindingList<double>();

Another reason might be that the upper line of code is within another thread. But I think this should not be the problem.

That's not good. You must make sure you don't do that because ListChanged event is expected to be raised on the UI thread.



回答2:

The better way is to clear the items and assign the DataSource again:

lstMeasuredValues.Items.Clear()// clear all items
lstMeasuredValues.DataSource = _measuredValues;

Or even you can define your own refresh function and call like the following:

public void RefreshList()
 {
   lstMeasuredValues.Items.Clear()// clear all items
   lstMeasuredValues.DataSource = _measuredValues;
 }

And call them when ever you need to refresh the list:

_measuredValues.Add(numBuffer);
RefreshList();
// Add more values 
RefreshList();


回答3:

The problem is that the common List isn't the right choice for data binding. You should use BindingList if you want to keep updated the ListBox.

Try using it this way:

BindingList<double> bindList = new BindingList<double>(_measuredValues);
lstMeasuredValues.DataSource = bindList;

Keep in mind that when you add a new item in _measuredValues you have to manually refresh the binding, as far as I now, like this:

bindList.ResetBindings();


回答4:

You could use a BindingList<double> as DataSource of your Listbox

List<double> _measuredValues = new List<double>();
BindingList<double> bindList = new BindingList<double>(_measuredValues);
lstMeasuredValues.DataSource = bindList;

Now everytime you need to add an element use the bindList variable and your listbox will update automatically as well as your _measuredValues list



回答5:

One of the simplest way to do it is by putting:

lstMeasuredValues.DataSource = null; //the cheapest, trickiest, but the most important line
lstMeasuredValues.DataSource = _measuredValues;

Whenever your _measuredValues element is updated



回答6:

All you need to do it to refresh the list after updating:

lstMeasuredValues.Refresh();