I was watching Craig Shoemaker’s video blog post on Introduction to XamDataGrid Excel Exporting and was excited that he started things out with a ViewModel but the export code was all performed in a code-behind. This left me wanting more. The problem is that Infragistics‘ DataPresenterExcelExporter requires you to pass a DataPresenterBase object (implemented by the XamDataGrid) as a parameter. This makes sense as the power of the DataPresenterExcelExporter feature is that whatever formatting you have put in place on your grid is what gets exported, saving me being asked to try and replicate a user’s grouping, sorting, or what-not in Excel.
So, I used a UserControl, and PRISM‘s generic DelegateCommand (one of my favorites) to pass the XamDataGrid instance through to my ViewModel and was able to move toward a more pure MVVM implementation of the export to excel feature.
You can find the complete sample here:
one concern here: the above solution is only good for cases where export button and grid are in the same view. How would I do if Export button is in say different view(like menuitem of container shell).
If button and grid are in same view then we can achieve the same without involving the viewModel since it would be within view.
In the case that you mention, where there is a need for inter-viewmodel communication, I have always resorted to using event subscription/publication processes. Prism, MVVMLight, and Caliburn all have an eventing framework available for just such a scenario.
At least that’s what I do.
Additionally, as soon as you are putting functionality withing the view (the button click for an export as well as the grid) and then handling the export within the view, you’ve now introduced an surface area within your code that is not easily testable through unit-testing means. The driving force behind all of the predominant UI technologies today (MVC, MVP, MVVM) is a focus on testability and the benefits that come with those patterns.
Hi Hunt, I’ve just read your post and wanted to take a look at your code but the link does not work. Could you fix it, please?
Apparently the file was removed from skydrive… I’ve posted an updated sample. It has a lot more in it than the original I posted so you’ll have to sift through it.
Great, thanks a lot!!