C# Force ListBox to update elements

前端 未结 2 929
说谎
说谎 2021-01-12 21:45

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 th

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 22:14

    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).

提交回复
热议问题