我正在继承标准 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 更新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!