绑定列表< Double>到WinForms-Listbox [英] Bind List<Double> to WinForms-Listbox
问题描述
列表< double> _measuredValues = new List< double>();
到一个winforms ListBox。
Form_Load我设置:
lstMeasuredValues.DataSource = _measuredValues;
当我更新值时,没有任何内容出现?
_measuredValues.Add(numBuffer);
我想到的一件事是数据类型问题。但是如何更改类型以将其更改为字符串?
lstMeasuredValues.DataSource = _measuredValues.ToString()。ToList ();
另一个原因可能是代码的上一行在另一个线程中。但我认为这不应该是问题。
如何绑定此列表?
当我更新值时,没有任何显示?
_measuredValues.Add(numBuffer);
为了允许UI反映数据源修改,数据源必须提供某种更改通知。 WinForms列表数据绑定基础架构使用 ListChanged事件 IBindingList接口。有一个标准提供了 BindingList List< T>
来获得所需的行为。所有你需要改变这一行
列表< double> _measuredValues = new List< double>();
至
BindingList< double> _measuredValues = new BindingList< double>();
另一个原因可能是代码的上一行在另一个线程。但我认为这不应该是问题。
这不好。 必须确保您不这样做,因为 ListChanged
事件预计将在UI线程上引发。
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?
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.
这篇关于绑定列表< Double>到WinForms-Listbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!