问题描述
我正在继承标准 ListBox 控件.我会收到有关添加到列表中的任何元素的更改的通知.任务是更新列表框显示的文本以用于更改元素.
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.
推荐答案
很遗憾,ListView 中的数据绑定不支持常规(项目)更改通知事件(FooChanged代码>/<代码>INotifyPropertyChanged代码>).但是,如果您知道更改,您可以让列表重新绑定自身.由于您是子类化,您可以调用:
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();
或单个项目:
this.RefreshItem(index);
否则,由于这不是公开的,您可以通过更改 DisplayMember 来模拟它:
Otherwise, since this isn't public, you can simulate it by changing the DisplayMember:
lb.DisplayMember = "";
lb.DisplayMember = "Bar";
可能有点 hacky,但它可以工作,并保持当前选择等(不像清除 DataSource).
A little hacky, maybe, but it works, and maintains the current selection etc (unlike clearing the DataSource).
这篇关于C# 强制 ListBox 更新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)