Opening a WPF window from an Excel add-in is now made easy using our helper method.
Objective: Open a WPF wizard from an Excel add-in and exchange data between Excel and the WPF window.
We are using the following AddinX packages:
- AddinX.Bootstrap: Register the WPF classes so the WPF window can be resolve without having to instantiate manually any dependency.
- AddinX.WPF: Provide a helper method to open a WPF window from within an Excel-DNA add-in.
- AddinX.Ribbon: Create a tab with buttons to call the WPF application
We will explain the part related to the WPF application, we assume that you are using the packages listed above. You can also download the sample from github or use the following command line.
Registration of the WCF class with the Inversion of Control container Autofac
If we want Autofac to be able to resolve the MainWindow from the WPF application, it is required to register all the WPF classes. It is done during the startup process of the Bootstrap.
Opening the WPF window
In the package AddinX.WPF, an helper class has been recreated to ease the process of opening of a WPF. Let consider that this form will be manage by a controller that receive as a parameter an IWpfHelper . Let’s call this controller “SampleController”.
The Inversion of Controls (IoC) container will resolve the dependency and inject the correct object when the controller will be instantiated as the WpfHelper class has been register to the IoC container.
The code associated with the opening of the WPF form is the following in the controller “SampleController”:
It is easy to link a button on the ribbon to call this method when clicked in the method CreateRibbonCommand of the class AddinRibbon:
Interaction between the WPF application and Excel
The interaction between the WPF application and Excel is possible as long they are sharing a communication channel like the events aggregator from Prism. This way the WPF application will be able to publish message and Excel will subscribe to them and return the appropriate response message.
In the sample, a controller has been created to manage the interaction with the WPF application: WpfInteractionController. This controller receives an instance of the IEventAggragtor and subscribe to specific events in its constructor
The token associated to each subscription is kept as it is important to unsubscribe from them when the controller is disposed to avoid memory leak.
Let’s look at the subscription to the second subscription which is regarding the name of the sheets of the current workbook. The information returned by Excel are used to populate the sheet destination combo-box of the WPF wizard.
The method GetWorksheetsName is as follow:
The code for the excelOperation.WorksheetsName() method is the following:
The request sent by the WPF application
For the current example regarding the worksheets name, the request is made from the constructor of the view model that need that information.