WPF 数据网格绑定
我正在使用 WPF 工具包数据网格,并且过去总是为网格创建要绑定的实体,例如具有名称、地址等的联系人实体。 在我正在开发的当前应用程序上,用户可以从 50 个表中进行选择,并从表中单独选择字段来生成视图。 显然,这里绑定一个实体是行不通的,因为绑定源是动态的。
问题是我该怎么办?
谢谢
I'm using the WPF toolkit datagrid and in the past have always created entities for the grid to bind to, so for example a Contact Entity with Name, Address etc.
On the current app I'm working on the user may select from 50 tables and individually select the fields from the tables to generate a view.
Clearly here having an Entity to bind to will not work as the binding source will be dynamic.
Question is what do I do?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚博客介绍了如何基于可重用模型为 DataGrid 动态创建列。
I just blogged about how to dynamically create columns for a DataGrid based on a reusable model.
最好的解决方案是使用
匿名类型
,它工作得很好,请参阅以下概念证明:The best solution is to use
Anonymous Types
it works perfectly, see the following proof of concept:一种方法是创建对象集合,并为每个对象提供一个自定义 TypeDescriptor。
当网格自动生成列时,它会使用对您的类(例如,Customer)的反射,并发现其属性(例如,FirstName、Balance 等)。
但这并不完全正确。 WPF 本身并不完成这项工作——它需要一个 TypeDescriptor。您还可以实现自己的 TypeDescriptor,这样您就可以假装拥有实际不存在的属性。或者就您的情况而言,假装不拥有确实存在的属性。
One approach would be to create a collection of objects, and give each object a custom TypeDescriptor.
When the grid is auto generating columns, it uses reflection over your class - e.g., Customer, and discovers its properties - e.g., FirstName, Balance, etc.
But that's not entirely true. WPF doesn't do the work itself - it asks a TypeDescriptor. And you can implement your own TypeDescriptor, so you can pretend to have properties that don't actually exist. Or in your case, pretend not to have properties that do exist.
您可以保留绑定源不变,但是您可以根据用户对他/她需要隐藏或查看的内容的偏好来过滤 DataGrid 的列。
You can leave binding source as it is, however you can filter DataGrid's columns based on user's preferences of what he/she needs to hide or see.