开发者

Monitoring lists using LiveBindings

I've been trying to find a generic way to notify a UI control of changes in a list. For example, when an object is added to a list I want it added automatically to the listbox. If an object is removed from the list, I want it removed automatically from the listbox.

Unfortunately there are some maddening inconsistencies between Delphi's lists: TList<>.Notify is fine but TStringList.OnChange doesn't even tell you what the change was and TList doesn't even have any notification whatsoever!

I was hoping LiveBindings would give me a proper way of monitoring changes in most lists, but so far I don't see it. All the examples I have seen fills the control and sets up bindings between the list items and the control items (listbox items in my example). But adding/removing an object from the list doesn't affect the control at all.

Does anyone know of a mechanism that I have missed, or more generally, a nice generic way to do this?

Clarification: It doesn't seem that I was clear enough before. The reason I need a generic way, is because I wrote a g开发者_如何学Pythonrid control that can connect to a variety of sources, including some existing code. I wrote an interface that the grid accepts as source and then a number of adapter classes to accept some lists and make them available as that interface. Since I needed to accept existing code as well, overriding TList.Notify is not an option. And since there is no event to see the changes, it effectively means TList does not have a notification mechanism that is usable by a client, such as my adapter class. TStrings doesn't have one either, but TStringList raises a simple OnChange, which means that the adapter class cannot actually determine what has changed.

I actually had a very nice solution that used the TVirtualMethodInterceptor, but that completely stopped working in Delphi XE and its not fixed in XE2.


You missed the TList.Notify mechanism.

TList has a protected Notify procedure that you must override. This is the way TObjectList works.


Bit late answer, but the generic TList has an OnNotify including the item added or removed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜