Integrate WPF Control in your Excel Solutions

In this article, I will show that it is possible to host a WPF control in a excel solution even that Visual studio and the .Net framework doesn't provide  us a way to host directly a given WPF control in an Excel application. To know how to realize such task, then follow this walkthrough.

Walkthrough:

1. Open VS 2008 and create a new project>WPF User Control Library:

WPF1.gif

Figure 1

2. Name the project WpfForExcel

3. In the XAML editor copy and paste this code

<UserControl x:Class="wpfcontrol.UserControl1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Height="273" Width="298" Loaded="UserControl_Loaded">

    <Grid Name="myGrid" Height="302" Width="298">

        <Image HorizontalAlignment="Right" Margin="0,11,10,151" Name="image1" Stretch="Fill" Width="111" Source="/wpfcontrol;component/Image.JPG" />

        <Label Margin="9,11,127,0" Name="label3" Foreground="White" Height="66" VerticalAlignment="Top">Developed by Bechir Bejaoui</Label>

    </Grid>

</UserControl>

4. Now, switch to the design mode and right click the control, then click view code

5. In the C sharp code editor the UserControl_Loaded stub is already generated then implement it with this code

System.Windows.Media.Brush oBrush;

        private void UserControl_Loaded(object sender, RoutedEventArgs e)

        {

            System.Windows.Media.Color oColor1 = System.Windows.Media.Colors.Red;

            System.Windows.Media.Color oColor2 = System.Windows.Media.Colors.Black;

        oBrush = new System.Windows.Media.LinearGradientBrush(oColor1, oColor2, 20);

           this.Background = oBrush;

        } 

6. Build the project WpfForExcel

7. Now, as Visual studio 2008 doesn't offer us a possibility to directly host a WPF control within Excel solution, we as compelled to leverage a classic windows user control then wrap the WPF control in it

8. Right click on the solution in the solution explorer then add a new project, a Windows user control project and name it WindowsControl.

9. Add a reference of the WPF control to the new Windows control, to do so, expand the Windows user control tree node in the solution explorer, then right click the references node and click the "Add Reference"

10. A windows like this appears:

WPF2.gif

Figure 2

11. Select the Browse tab, and then browse to the WPF control/Debug repertory, there you can find the dll assembly.

12. Select it, and then click OK.

13.  In the top of the tool box you will find a new tab that holds the same name as the WPF control directory.

14.  Expand it, then drag and drop the WPF user control into the windows control and dimension it so that it fully appears within the windows control.

15. Build this windows control project

16. Now, add a new Excel add-in project and name it myExcelAddIn

WPF3.gif

Figure 3

17. Then, add a reference of the windows user control "WindowsControl" to the new Excel add-in project

18. Add a Microsoft.Office.Tools namespace to the namespace list.

19.  Do implement the private void ThisAddIn_Startup(object sender, System.EventArgs e)and private void ThisAddIn_Shutdown(object sender, System.EventArgs e) as follows

CustomTaskPane myCustomTaskPane;

WindowControl.UserControl1 oControl;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

   oControl = new WindowControl.UserControl1();

   myCustomTaskPane = this.CustomTaskPanes.Add(oControl,"WPF by Bechir Bejaoui");

   myCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;

   oControl.Dock = System.Windows.Forms.DockStyle.Fill;

   myCustomTaskPane.Width= 390;

   myCustomTaskPane.Height = 450;

   myCustomTaskPane.Visible = true;

}

 

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

    myCustomTaskPane.Dispose();

}

 

Finally, set the Excel add-in project as start up project and build the entire solution and let's fire up the application.

This is the result:

WPF4.gif

Figure 4
 
Good Dotneting !!!

Up Next
    Ebook Download
    View all
    Learn
    View all