简体   繁体   English

绑定清单 <Double> 到WinForms-Listbox

[英]Bind List<Double> to WinForms-Listbox

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. 到winforms ListBox。

In Form_Load I set: 在Form_Load中,我设置:

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? 如何绑定此列表?

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();

When I update the values, nothing appears?! 更新值时,什么都没有出现?

_measuredValues.Add(numBuffer); _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. 为了使UI能够反映数据源的修改,数据源必须提供某种变更通知。 WinForms list data binding infrastructure uses ListChanged event of the IBindingList Interface . WinForms列表数据绑定基础结构使用IBindingList接口的 ListChanged事件 There is a standard provided BindingList<T> class which can be used instead of List<T> to get the desired behavior. 提供了一个标准的BindingList <T>类 ,可以使用它代替List<T>以获得所需的行为。 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. 必须确保不要这样做,因为预期UI线程上会引发ListChanged事件。

The problem is that the common List isn't the right choice for data binding. 问题在于,通用List不是数据绑定的正确选择。 You should use BindingList if you want to keep updated the ListBox. 如果要保持更新ListBox,则应使用BindingList

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: 请记住,当您在_measuredValues中添加新项目时,必须手动刷新绑定,直到现在为止,如下所示:

bindList.ResetBindings();

You could use a BindingList<double> as DataSource of your Listbox 您可以将BindingList<double>用作列表框的数据源

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 现在,每次您需要添加元素时,请使用bindList变量,列表框以及_measuredValues列表将自动更新

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 每当您的_measuredValues元素更新时

您需要执行的所有操作以在更新后刷新列表:

lstMeasuredValues.Refresh();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM