C# Force ListBox to update elements

爱⌒轻易说出口 提交于 2019-12-01 02:09:26

问题


I'm subclassing the standard ListBox control. I get notified of changes to any of the elements added to the list. The task is to update the text shown by the ListBox for the changing element.

I'm aware that i could just remove the changed element and add it again, but this seems not preferable for obvious reasons.


回答1:


Unfortunately, the data-binding in ListView doesn't support regular (item) change notification events (FooChanged / INotifyPropertyChanged). However, if you know about the change, you can get the list to re-bind itself. Since you are subclassing, you can call:

this.RefreshItems();

or for a single item:

this.RefreshItem(index);

Otherwise, since this isn't public, you can simulate it by changing the DisplayMember:

lb.DisplayMember = "";
lb.DisplayMember = "Bar";

A little hacky, maybe, but it works, and maintains the current selection etc (unlike clearing the DataSource).




回答2:


Why don't you manually update Text of an item in question? You might also consider rolling out your own databinding mechanism for ListBox. And check out ObjectListView to see if it's of any help.



来源:https://stackoverflow.com/questions/519538/c-sharp-force-listbox-to-update-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!