Sdílet prostřednictvím


Using WPF Controls in Office Solutions

Although solutions created by using the Office development tools in Visual Studio are designed to work with directly with Windows Forms controls, you can also use WPF controls in your 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 WPF.

Applies to: The information in this topic applies to document-level projects and application-level projects for Microsoft Office 2010 and the 2007 Microsoft Office system. For more information, see Features Available by Office Application and Project Type.

Any UI element that can host Windows Forms controls in an 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.

link to video For a related video demonstration, see How Do I: Use WPF Controls Inside an Excel Actions Pane?.

Adding WPF Controls to Office Projects at Design Time

You cannot add WPF controls directly to UI elements in 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

  1. Open a project to which you want to add a custom task pane, an actions pane, or a form region.

  2. Add a User Control (WPF) item to your project.

  3. 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.

  4. Build the project.

  5. Add an actions pane, form region, or custom task pane to your project:

  6. 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.

  7. Rebuild the project.

To add WPF controls to a document or worksheet in a document-level project

  1. Open a document-level project for Word or Excel.

  2. Add a User Control (WPF) item to your project.

  3. From the Toolbox, add WPF controls to the WPF user control design surface.

  4. Build the project.

  5. Add a User Control item (that is, a Windows Forms user control) to the project.

  6. Open the designer for the Windows Forms user control.

  7. 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.

  8. 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.

    Poznámka

    You cannot drag the Windows Forms user control to the document or worksheet in the designer.

  9. Rebuild the project.

Hosting WPF Controls by Using the ElementHost Class

Visual Studio provides features that help you use Windows Forms controls in your Office 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. 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 WPF 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:

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

Concepts

Windows Forms Controls on Office Documents Overview

Other Resources

Office UI Customization

Actions Pane Overview

Custom Task Panes Overview

Creating Outlook Form Regions