C#强制ListBox更新元素 [英] C# Force ListBox to update elements

查看:349
本文介绍了C#强制ListBox更新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将标准ListBox控件子类化.我收到有关添加到列表的任何元素的更改的通知.该任务是为更改元素更新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更新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆