将 WPF Infragistics Excel 导出功能与 MVVM 结合使用
我正在使用 Infragistics WPF 数据网格,并且我想利用 Infragistics 提供的 Excel 导出功能。用于 Excel 导出的 API 需要网格实例作为参数。这对 MVVM 不太友好,因为我更喜欢处理在 viewModel 中导出数据的命令,而该命令不知道我视图中的 datagrid 控件的实例。
有谁知道使用 Infragistics 库进行 excel 导出(最好基于网格绑定到的集合)的更 MVVM 友好的机制?
I'm using the Infragistics WPF datagrid and I want to make use of the Excel export functionality that Infragistics provides. The API for the Excel export requires an instance of the grid as a parameter. This isn't very MVVM friendly since I'd prefer to handle the command to export the data in my viewModel which wouldn't know about the instance of the datagrid control in my view.
Does anyone know of a more MVVM friendly mechanism to do excel exports (preferable based on the collection the grid is bound to) using the Infragistics libraries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如您所知,由于需要引用网格,因此不应将逻辑放入视图模型中。您只需要在代码隐藏中处理导出命令。这样做在概念上并没有什么错误,也没有什么反 MVVM 的地方:这个特定的逻辑恰好是特定于视图的。
Due to the required reference to the grid, as you know, you should not put the logic into the View Model. You just need to handle the Export command in your code-behind. There is nothing conceptually wrong, or anti-MVVM, about doing this: this particular logic just happens to be View-specific.
数据网格是视图的一部分,导出到 Excel 是该网格的一个功能。根本没有 ViewModel 的参与。它是视图活动的纯粹案例。考虑到这是纯视图功能,您不能/不需要在 ViewModel 中处理导出命令,而是在视图(代码隐藏)本身中处理。
明天,如果您将此数据网格替换为另一个数据网格,则导出到 Excel 的功能也将消失,因为它是由 infragistics 提供的。因此你的视图模型不应该做任何事情。但是,如果您想实现不特定于基础设施的通用且独立的功能,您可以在 ViewModel 中实现(因为它是通用的且不依赖于基础设施)。
The datagrid is part of view and export to excel is a feature of this grid. There is no involvement of ViewModel at all. Its a pure case of View activity. Considering this as apure view feature, you cannot / need not handle the Export Command in ViewModel but in the View (code-behind) itself.
Tomorrow if you replace this datagrid with another datagrid, the export to excel feature will also go, since its provided by infragistics. Hence your view model is not supposed to do anything. However if you want to implement a generic and independent functionality which is not specific to infragistics, you may do it in the ViewModel (Because its generic and not dependent of infragistics).
简单地说,
您可以像这样在 xaml 中添加按钮,并使用命令参数来传递网格。
并在您的视图模型中添加以下命令:
simply ,
you can add button in ur xaml like this and use command parmeter to pass grid.
and in ur view model add this command: