Using WPF Controls in Office Solutions
Windows Presentation Foundation (WPF) is an alternative to Windows Forms for designing user interfaces. WPF uses a markup language called Extensible Application Markup Language (XAML) to provide new techniques for incorporating UI, media, and documents. For more information, see Introduction to Windows Presentation Foundation.
Although Visual Studio Tools for Office solutions are designed to work with Windows Forms controls, you can also use WPF controls in your solutions. Any UI element that can host Windows Forms controls in a Visual Studio Tools for Office solution can also host WPF controls. These include the following elements:
Documents and worksheets in document-level customizations.
Actions panes in document-level customizations.
Custom task panes in application-level add-ins.
Form regions in application-level add-ins for Outlook.
For a related video demonstration, see How Do I: Use WPF Controls Inside an Excel Actions Pane?.
Adding WPF Controls to Visual Studio Tools for Office Projects at Design Time
You cannot add WPF controls directly to UI elements in Visual Studio Tools for Office solutions. Instead, add a User Control (WPF) item to your project, and use it as the design surface for WPF controls. Then, add the WPF user control to a UI element in your project.
To add WPF controls to an actions pane, custom task pane, or form region
Open a project to which you want to add a custom task pane, an actions pane, or a form region.
Add a User Control (WPF) item to your project.
From the Toolbox, add WPF controls to the WPF user control design surface.
By default, when the WPF user control designer is open, the Toolbox contains only WPF controls.
Build the project.
Add an actions pane, form region, or custom task pane to your project:
For form regions, add an Outlook Form Region item to the project. For more information, see How to: Add a Form Region to an Outlook Add-in Project.
For actions panes, add an Actions Pane Control or User Control item to the project. For more information, see How to: Add an Actions Pane to Word Documents and How to: Add an Actions Pane to Excel Workbooks.
For custom task panes, add a User Control item to the project. For more information, see How to: Add a Custom Task Pane to an Application.
From the ProjectName WPF User Controls tab of the Toolbox, drag the WPF user control to the designer for the actions pane, form region, or custom task pane.
Visual Studio automatically creates an ElementHost object that hosts the WPF user control on the UI element.
Rebuild the project.
To add WPF controls to a document or worksheet in a document-level project
Open a document-level project for Word or Excel.
Add a User Control (WPF) item to your project.
From the Toolbox, add WPF controls to the WPF user control design surface.
Build the project.
Add a User Control item (that is, a Windows Forms user control) to the project.
Open the designer for the Windows Forms user control.
From the ProjectName WPF User Controls tab of the Toolbox, drag the WPF user control to the designer.
Visual Studio automatically creates an ElementHost object that hosts the WPF user control in the Windows Forms user control.
Write code that programmatically adds the Windows Forms user control to the document or workbook. For more information, see Adding Controls to Office Documents at Run Time.
Note
You cannot drag the Windows Forms user control to the document or worksheet in the designer.
Rebuild the project.
Hosting WPF Controls by Using the ElementHost Class
Visual Studio Tools for Office provides features that help you use Windows Forms controls in your solutions, but it does not provide similar features for WPF controls. For example, you can add Windows Forms controls to documents and worksheets at design time by dragging controls from the Toolbox, or at run time by using helper methods. However, these tools are not available for WPF controls.
WPF controls use the ElementHost class as an integration layer between a Windows Forms control or form and the WPF controls. The ElementHost class is provided by the .NET Framework, beginning with version 3.0.
When you add WPF controls to your solution at design time, Visual Studio automatically generates an ElementHost object for you. For a walkthrough that demonstrates how to use the ElementHost class programmatically, see How to: Host a Windows Presentation Foundation Control in Windows Forms by Using ElementHost.
WPF Resources
For more information about architectural and design issues for hosting WPF controls on Windows Forms controls and forms, see the following topics:
For more information about adding WPF controls to Windows Forms controls and forms in Visual Studio at design time, see the following topics:
Walkthrough: Creating New Windows Presentation Foundation Content on Windows Forms at Design Time
Walkthrough: Arranging Windows Presentation Foundation Content on Windows Forms at Design Time
Walkthrough: Styling Windows Presentation Foundation Content
See Also
Tasks
How to: Add an Actions Pane to Word Documents
How to: Add an Actions Pane to Excel Workbooks
How to: Add a Custom Task Pane to an Application
How to: Add a Form Region to an Outlook Add-in Project