如何将 ExpandoObject 集合绑定到数据网格?
我正在尝试从 Excel 文件 (.xls) 读取表格并将其显示在 DataGrid 中。该表的维度未知,每列都有一种未知类型的值(字符串、双精度或整数)。
我通过 COM 访问该文件并将表放入 List<> 中ExpandoObjects 的。当我将 DataGrid.ItemSource 设置为列表时,网格在视觉上仍然是空的。 显式定义列及其数据绑定会生成应用程序无法在 ExpandoObjects 中找到指定属性的运行时消息。
如何在 GridView 中显示表格?我使用 Silverlight 5 RC,并希望有一种简单的方法来做到这一点。至少比我迄今为止看到的 Silverlight 2 和 3 的解决方案更简单。
I'm trying to read a table from an Excel file (.xls) and display it in a DataGrid. The table has unknown dimensions and each column has values of one unknown type (string, double or int).
I access the file via COM and put the table in a List<> of ExpandoObjects. When I set DataGrid.ItemSource to the List the Grid remains visually empty.
Explicitly defining columns and their data binding yields the runtime message that the application can't find the specified properties in the ExpandoObjects.
How can I display the table in the GridView? I work with Silverlight 5 RC and was hoping for a simple way to do it. At least simpler than the solutions I saw for Silverlight 2 and 3 so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它是动态的并且未实现 ICustomTypeProvider,在 Silverlight 5 中它不会绑定。这确实很不幸,因为我们在 WPF 中具有动态数据绑定,并且在属性已知的情况下(如 Expando),甚至编写适用于任何 IDynamicMetaObject 提供程序的 CustomType 并不困难,更不用说他们可以直接添加它对于 Expando 来说,尤其是因为它是密封的。
所以底线是你需要编写自己的 动态类型实现 ICustomTypeProvider
If it's dynamic and it doesn't implement ICustomTypeProvider, in Silverlight 5 it will not bind. It's really unfortunate since we have dynamic databinding in WPF, and in the case that properties can be known (like Expando), even writing a CustomType that would work for any IDynamicMetaObject provider isn't difficult, not to mention they could have just added it for Expando, especially since it's sealed.
So the bottom line is that you need to write your own dynamic type implementing ICustomTypeProvider
我意识到 Vladimir Bodurov 的 解决方案 效果很好为我。我用 Dictionaries 替换了 ExpandoObjects,并使用 Bodurov 的类将 List 转换为 DataGrid 可以处理的内容。
I realized Vladimir Bodurov's solution works fine for me. I replaced the ExpandoObjects by Dictionaries and used Bodurov's class to transform the List into something the DataGrid can process.