Click here to Skip to main content
6,292,426 members and growing! (11,785 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Intermediate License: The Code Project Open License (CPOL)

Use WPF Controls in VSTO Solutions with Visual Basic 2008

By Alessandro Del Sole

An explanation of how to add WPF controls to Visual Studio Tools for Office solutions, writing Visual Basic code
VB (VB 9.0), .NET (.NET 3.5), Visual Studio (VS2008), WPF
Posted:9 Mar 2008
Views:13,856
Bookmarked:8 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
2 votes for this article.
Popularity: 1.10 Rating: 3.67 out of 5

1

2
1 vote, 50.0%
3

4
1 vote, 50.0%
5
WPFControlsExcelWorkbook

Introduction

The Visual Studio 2008 Tools for Office System allow to drag and drop Windows Forms controls onto Office documents’ surface to create an advanced user experience.

It is also possible to use WPF controls, but this technique requires some more steps, because Office applications cannot directly host WPF controls. So we must use interoperability between the two technologies.

In this article, I will show you how to create a custom Windows Forms control to host WPF controls which can be dragged onto Office 2007 documents’ surface.

Background

You should be quite skillful with WPF controls and .NET user controls before you read this article.

Using the Code

Open Visual Studio 2008 and create a new Visual Basic 2008 document-level solution for Microsoft Excel 2007.

The first step is to add to the solution a new custom Windows Forms control. After you added one, rename it as HostUserControl.vb. Then, from the WPF Interoperability tab in Visual Studio 2008 toolbox, drag an ElementHost item to the User Control surface. Set the Dock property of the ElementHost to Fill.

Once you've done this, add a reference to the following WPF assemblies:

  • WindowsBase.dll
  • PresentationCore.dll
  • PresentationFramework.dll

At this point, we can go to implement a WPF control writing managed code (in this case we cannot obviously accomplish this by XAML). For example, we could add to our User Control an Expander control, which is provided by the System.Windows.Controls namespace in WPF. Select the user control and switch to the code editor, then type the following code:

Imports System.Windows.Controls
Imports System.Windows.Media

Public Class HostUserControl

    Private Sub HostUserControl_Load(ByVal sender As Object, 
ByVal e As System.EventArgs) Handles Me.Load
        Dim ex As New Expander

        ex.Header = " WPF control in Excel 2007"
        ex.Background = New SolidColorBrush(Colors.LightBlue)
        ex.BorderBrush = New SolidColorBrush(Colors.Black)
        ex.BorderThickness = New Windows.Thickness(2)

        Me.ElementHost1.Child = ex
    End Sub
End Class

Now compile the solution, so that the toolbox gets updated with the new user control. Switch to design mode and drag the new HostUserControl onto an Excel 2007 sheet. The above figure shows the result.

Once you have designed your control, you can write code to handle its events in the same way you use to do with other controls.

Points of Interest

As you can see, using WPF controls in VSTO solutions is a substantially simple procedure, even if it's not possible to use XAML to manage controls UI. Anyway, you will obtain amazing results also by writing managed code.

History

  • 9th March, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alessandro Del Sole


Member
I'm a Microsoft Visual Basic MVP. I'm an Italian .NET developer and I write articles and books about Visual Basic 2005/2008, WPF and VSTO.

Check out my blog at: http://community.visual-basic.it/AlessandroEnglish
Occupation: Other
Location: Italy Italy

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by Alessandro Del Sole
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project