ListBox 不显示数据源的更改
我以为这是一个简单的问题,但我在网上找不到任何信息。我使用 BindingSource
将 ListBox 绑定到 List
,如下所示:
List<Customer> customers = MyMethodReturningList();
BindingSource customersBindingSource = new BindingSource();
customersBindingSource.DataSource = customers;
customersListBox.DataSource = customersBindingSource;
现在,当我从 customers
列表中添加或删除时,我的 ListBox
会更新(即使没有在 BindingSource
上使用 ResetBindings
),但如果我更改列表中的任何客户对象,它也不会更新。调用 ResetBindings
没有任何效果。我什至实现了自己的 BindingList
,但行为并没有改变。Customer
类使用属性来访问和修改数据。其 ToString()
内容显示在列表中。
我在 .Net 2.0 中使用 C#。
有什么想法吗?
谢谢
I thought this was a simple problem, but I can't find any information on the web. I'm binding a ListBox to a List
using BindingSource
like so:
List<Customer> customers = MyMethodReturningList();
BindingSource customersBindingSource = new BindingSource();
customersBindingSource.DataSource = customers;
customersListBox.DataSource = customersBindingSource;
Now, when I add or delete from customers
list, my ListBox
gets updated (even without using ResetBindings
on BindingSource
), but if I change any of the customer objects in the list, it does not. Calling ResetBindings
has no effect. I even implemented my own BindingList
, but the behaviour hasn't changed.
The Customer
class uses properties for accessing and modification of data. Its ToString()
content is displayed in the list.
I'm using C# in .Net 2.0.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用
BindingList
,您甚至不需要BindingSource
:If you use a
BindingList
you don't even need theBindingSource
:好的,这是一个肮脏的修复:无论何时您都需要刷新设置 datasource = null 的框内容,然后重新绑定它。
它不更新的原因是列表中的对象没有更改,并且它只检查对象的引用而不是其内容。
OK, here's a dirty fix: wenever you need to refresh the box contents set datasource = null, then rebind it.
the reason it doesn't update is because the objects in the list haven't changed and it only checks the refrences of the object rather than their contents.
列表框中还有一个错误可能会导致此问题。如果将 SelectionMode 设置为 None,则会出现此问题。
作为解决方法,我将选择模式设置为“One”,然后在更新数据源时返回“None”。
There is also a bug in the list box which can cause this problem. If you set the SelectionMode to None this problem appears.
As a work around I set the selection mode to One and then back to None when updating the datasource.
我通过在更新源时将数据转换为数组解决了这个问题。请参阅 UpdateData 方法。这样您就可以更新组合框而不会丢失组合框设置。
I got around this problem by converting data to array when updating source. Please see UpdateData method. This way you can update your combo box without losing ComboBox Settings.
据我所知,这个问题是在大约 6 年前提出的,但除了解决方法之外,我在这里没有看到正确的答案。
当您更改集合中项目的属性时,会为元素(对象)引发事件,但不会为集合引发事件。因此集合不会看到变化,也不会刷新绑定的控件。所有绑定集合和大多数通用集合(例如
List
)内的元素都会接收 2 个事件:PropertyChanging
和PropertyChanged
。当集合内元素的属性发生更改时,会触发该事件。您需要做的就是添加一个事件处理程序,该处理程序将触发重新绑定或引发Collection
上的事件。I understand that this question was asked almost 6 years ago but other than work-arounds I do not see a correct answer here.
When you change property of an item in a collection the event gets raised for the element (object) but not the collection. So the collection does not see a change and will not refresh bound controls. Elements inside all binding collections and most generic collections like
List<>
receive 2 events,PropertyChanging
andPropertyChanged
. When a property of the element inside collection is changed, the event gets triggered. All you need to do is add an event handler that would trigger either re-binding or raise an event on theCollection
.