Hello,
I have a XamRibbonGroup with a ButtonTool that binds to an ICommand in the ViewModel. This command would execute the DataPresenterExcelExporter.Export.
However this builtin method needs a reference to the XamDataGrid (DataPresenterBase)in order to export the data.
How can I reference the XamDataGrid using MVVM from the Command ?
Thank you
I have figured that one out.
Hello User101,
Thank you for your update. I am glad you were able to figure out how to push the XamDataGrid to your Export command.
I imagine you are likely using an ElementName binding, but if not, I have also attached a sample project that I had created that demonstrates how this could be done.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Hello Andrew,
The solution I originally implemented had some issues.
I'm trying to adapt your solution. However, I have one XamRibbonGroup with a ButtonTool that acts on many views.In another words the XamRibbonGroup and the XamDataGrid are not in the same view. The export function needs the reference to that XamDataGrid.
MVVM makes things more challenging sometimes. Any idea ?
Have you had a chance to look at this issue ?
I have done an initial investigation into your most recent requirement, and in order to do this, I would recommend that you keep a List of XamDataGrids and bind this list to your ButtonTool. Perhaps to do this you could keep an underlying "helper" class object that could be accessed via the data context of both your ButtonTool and each of the XamDataGrids in your application.
In the attached behaviors that you had mentioned that you had written for your XamDataGrids, you could then access this underlying class object and if you wish for the associated XamDataGrid to be exported, you could add or remove it to or from this List that exists on this underlying helper class. As long as your ButtonTool then knows about the underlying helper class, you can export each of your XamDataGrids this way.
Perhaps this underlying helper class could be stored as a resource in the Application.Resources in your App.xaml? This would give you the ability to access it from any point in your application by its key-name. You could then write a Behavior<ButtonTool> which accesses this helper class and exports each of the XamDataGrids on click. Note, this is all under abstract at the moment, as I have not built a sample project that does this, but what is outlined here should work for you on this matter.
Thank you for clarifying your requirement a bit on this matter.
I believe for you to be able to do this and keep it MVVM, you will likely need a container of some sort that can keep information about what the actual "active view" is. You would then bind your ButtonTool to this container's reference to the active view so that you can get that particular view when you go to export. Then, when you go to export, you could look at the content of your active view and parse through the visual tree of it to find your XamDataGrid and export it. The Infragistics.Windows.Utilities class could help with this visual tree traversal via its GetDescendantFromType and GetDescendantFromName methods. For example, you could do something like the following, where "active" would be the active view:
XamDataGrid grid = Utilities.GetDescendantFromType(active, typeof(XamDataGrid), false) as XamDataGrid;
Or, if the XamDataGrid has a name...
XamDataGrid grid = Utilities.GetDescendantFromName(active, "nameOfXamDataGridHere") as XamDataGrid;
From there, you could export that particular grid. Note, that if no grid is found as a descendant, the Utilities class will return null.
As for the actual "container" in this case, perhaps the XamDockManager control could help you here? The XamDockManager has an "ActivePane" property that determines the pane that is currently "focused." In this case, the Content of this ActivePane would be your "active view." Perhaps you could bind your ButtonTool's CommandParameter to the XamDockManager using an ElementName binding, and a path to the ActivePane.Content? Something like the following may work for you here:
<ButtonTool CommandParameter="{Binding ElementName=dockManagerName, Path=ActivePane.Content"}"/>
You can read further about the XamDockManager control here: https://es.infragistics.com/help/wpf/xamdockmanager. I hope this helps.
Thank you for the reply.
I might have got you lost. I'm not trying to export a group of XamDataGrids. It is only one grid at a time.
I have a RibbonGroup with many ButtonTools that binds to a viewModel of the an active view.
Swtiching views would switch the viewModel. I got all that working.
One ButtonTool is responsible to export the XamDataGrid in the active view.
The export function needs the reference to that XamDataGrid. How can I get a reference to that XamDataGrid without breaking the MVVM pattern ?