DataGrid 如何绑定到任何集合的属性?

发布于 2024-12-04 19:29:13 字数 317 浏览 0 评论 0原文

我正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛刀软妹 2024-12-11 19:29:13

这是一个复杂的区域;以下是 winforms 绑定的细分,但我知道 WPF 绑定并没有那么不同;

  • 给定一个对象,绑定代码要做的第一件事就是查找 IListSource - 如果是,则使用 GetList() 来获取实际的绑定(这就是数据表的方式)成为用于绑定目的的数据视图)
  • 之后,通常会检查源中的 ITypedList;这是获取模型表示的伪属性 (GetItemProperties()) 的灵活方法; data-view 实现 ITypedList,为每个实例创建伪属性
  • ,否则,可能会识别数据的明显类型,这可能意味着:
    • 寻找 public SomeType this[int index] {get;} 形式的索引器 - 请注意,大多数集合都会满足此要求
    • (不是由 winforms 完成,但可能用于 WPF)通过反射解析 IListIEnumerable 中的 T code> 如果对象实现了这些接口
    • 从源中获取第一个对象(如果有),并使用 GetType()
  • 一旦类型已知,TypeDescriptor.GetProperties(type) 可用于获取属性;在许多情况下,这将通过反射实现,但也可以添加间接层(通过 TypeDescriptionProvider )来提供类型的属性(这可以在运行时添加,这非常方便)
  • 在单独绑定(不是列表绑定)的情况下,还有 TypeDescriptor.GetProperties(obj) - 除了反射和 TypeDescriptionProvider 之外,这还支持ICustomTypeDescriptor 可以由单个对象实现以在运行时提供自定义属性(与 TypeDescriptionProvider 非常相似,但单个对象负责属性)

我不知道我确切地知道其中有多少适用于 WPF 绑定,但我很确定 IListSourceITypedList 处理是相同的。根据记忆,大多数(全部?)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;

  • given an object, the first thing binding code will do is look for IListSource - and if so use GetList() to get the actual binding (this is how a data-table becomes a data-view for binding purposes)
  • after that, the source is typically checked for ITypedList; this acts as a flexible way of obtaining pseudo-properties (GetItemProperties()) represented by the model; data-view implements ITypedList, creating pseudo-properties per instance
  • otherwise, it might be possible to identify an obvious type for the data, which can mean:
    • looking for an indexer of the form public SomeType this[int index] {get;} - note that most collections will satisfy this
    • (not done by winforms, but might be for WPF) resolving via reflection the T in either IList<T> or IEnumerable<T> if the object implements those interfaces
    • taking the first object (if any) from the sorce, and using GetType()
  • once a type is known, 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 (via TypeDescriptionProvider) to supply properties for a type (this can be added at runtime, which can be very convenient)
  • in the case of individual binding (not list binding), there is also TypeDescriptor.GetProperties(obj) - in addition to reflection and TypeDescriptionProvider, this also has support for ICustomTypeDescriptor which can be implemented by an individual object to supply custom properties at runtime (very similar to TypeDescriptionProvider, 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 and ITypedList 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.

掐死时间 2024-12-11 19:29:13

我认为

你可以获取其Collection元素的Type,这个类型TypeGetProperties() 方法,该方法返回其类型中可用的所有公共属性,并且在其之后知道所有公共属性,它可以绑定 他们。

I think that:

you can get Type of element of its Collection, this type Type have GetProperties() method, which returns all public properties available in its type, and, after its know all public properties, it can bind with them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文