DataGrid 如何绑定到任何集合的属性?
我正在制作一个 WPF 用户控件,并且我希望在绑定意义上与 DataGrid 控件具有类似的行为。我的问题是:DataGrid 如何知道如何绑定到 IEnumerable 类型的任何集合?例如:您可以将 DataView 作为 ItemsSource 传递,也可以传递任何对象集合。 DataGrid 如何决定是绑定到 DataView 的列,还是绑定到对象的属性,只需查看以下内容:
<DataGridTextColumn Binding="{Binding **Path=SomePropertyOrColumn**}"/>
提前致谢。
I am making a WPF user control and I want similar behavior as DataGrid control in sense of binding. My question is: How does DataGrid know how to bind to any collection of type IEnumerable? For example: You can pass a DataView as ItemsSource, and you can also pass any object collection. How DataGrid decides whether to bind to a column of DataView, or to a property of object only by looking at this:
<DataGridTextColumn Binding="{Binding **Path=SomePropertyOrColumn**}"/>
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个复杂的区域;以下是 winforms 绑定的细分,但我知道 WPF 绑定并没有那么不同;
IListSource
- 如果是,则使用GetList()
来获取实际的绑定(这就是数据表的方式)成为用于绑定目的的数据视图)ITypedList
;这是获取模型表示的伪属性 (GetItemProperties()
) 的灵活方法; data-view 实现 ITypedList,为每个实例创建伪属性public SomeType this[int index] {get;}
形式的索引器 - 请注意,大多数集合都会满足此要求IList
或IEnumerable
中的T
code> 如果对象实现了这些接口GetType()
TypeDescriptor.GetProperties(type)
可用于获取属性;在许多情况下,这将通过反射实现,但也可以添加间接层(通过 TypeDescriptionProvider )来提供类型的属性(这可以在运行时添加,这非常方便)TypeDescriptor.GetProperties(obj)
- 除了反射和TypeDescriptionProvider
之外,这还支持ICustomTypeDescriptor
可以由单个对象实现以在运行时提供自定义属性(与TypeDescriptionProvider
非常相似,但单个对象负责属性)我不知道我确切地知道其中有多少适用于 WPF 绑定,但我很确定
IListSource
和ITypedList
处理是相同的。根据记忆,大多数(全部?)winforms 策略都可以在 WPF 上运行 - 因此可能是这样,并且更改是额外的钩子。This is a complex area; the following is a breakdown from winforms binding, but I understand that WPF binding isn't that different;
IListSource
- and if so useGetList()
to get the actual binding (this is how a data-table becomes a data-view for binding purposes)ITypedList
; this acts as a flexible way of obtaining pseudo-properties (GetItemProperties()
) represented by the model; data-view implementsITypedList
, creating pseudo-properties per instancepublic SomeType this[int index] {get;}
- note that most collections will satisfy thisT
in eitherIList<T>
orIEnumerable<T>
if the object implements those interfacesGetType()
TypeDescriptor.GetProperties(type)
can be used to obtain properties; in many cases this will be via reflection, but it is also possible to add an indirection layer (viaTypeDescriptionProvider
) to supply properties for a type (this can be added at runtime, which can be very convenient)TypeDescriptor.GetProperties(obj)
- in addition to reflection andTypeDescriptionProvider
, this also has support forICustomTypeDescriptor
which can be implemented by an individual object to supply custom properties at runtime (very similar toTypeDescriptionProvider
, but with the individual object taking responsibility for the properties)I don't know exactly how much of this applies to WPF binding, but I'm pretty sure the
IListSource
andITypedList
processing is identical. From memory, most (all?) of the winforms strategies will work on WPF - so it could be that and changes are additional hooks.我认为:
你可以获取其
Collection
元素的Type
,这个类型Type
有GetProperties() 方法,该方法返回其类型中可用的所有公共属性,并且在其之后知道所有公共属性,它可以绑定 他们。I think that:
you can get
Type
of element of itsCollection
, this typeType
have GetProperties() method, which returns all public properties available in its type, and, after its know all public properties, it can bind with them.