How can I databind to properties not associated with a list item in classes deriving List

前端 未结 3 819
無奈伤痛
無奈伤痛 2021-01-13 16:31

Previously, I had a class that wrapped an internal System.Collections.Generic.List (where Item is a class I created). The wrapper class provided se

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 16:59

    The system treats anything that implements IList (or IListSource) as a container, rather than an item. As such, you cannot bind to properties of anything that implements IList. As such, encapsulation (i.e. what you already have) is the best approach if you want to be able to bind to properties of the container.

    However, you should note that many bindings support dot-notation in the source - i.e. either binding to "Items.SomeProperty", or setting the auxiliary property (typically DataMember) to specify sub-lists.

    This allows you to have a single BindingSource, and have different controls bound to different levels in the hierarchy - i.e. you might have a TextBox bound to AverageValue, and a DataGridView (with the same DataSource) that has DataMember="Items".

提交回复
热议问题