* net-current-v30plus-md.md * net-current-v40plus-md.md * tla2sharptla-ui-md.md * tla2sharptla-uiautomation-md.md * tla2sharptla-winclient-md.md * tla2sharptla-xaml-md.md * tlasharptla-ui-md.md * tlasharptla-uiautomation-md.md * tlasharptla-winclient-md.md * tlasharptla-xaml-md.md * fix warnings
3.5 KiB
title, description, titleSuffix, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | description | titleSuffix | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||
|---|---|---|---|---|---|---|---|---|---|
| Host a Windows Forms control in WPF | Learn how to host Windows Forms controls in Windows Presentation Foundation, which already provides many controls with a rich feature set. | 03/30/2017 |
|
|
9cb88415-39b0-4c46-80c4-ff325b674286 |
Walkthrough: Hosting a Windows Forms Control in WPF
WPF provides many controls with a rich feature set. However, you may sometimes want to use Windows Forms controls on your WPF pages. For example, you may have a substantial investment in existing Windows Forms controls, or you may have a Windows Forms control that provides unique functionality.
This walkthrough shows you how to host a Windows Forms xref:System.Windows.Forms.MaskedTextBox?displayProperty=nameWithType control on a WPF page by using code.
For a complete code listing of the tasks shown in this walkthrough, see Hosting a Windows Forms Control in WPF Sample.
Prerequisites
You need Visual Studio to complete this walkthrough.
Hosting the Windows Forms Control
To host the MaskedTextBox control
-
Create a WPF Application project named
HostingWfInWpf. -
Add references to the following assemblies.
-
WindowsFormsIntegration
-
System.Windows.Forms
-
-
Open MainWindow.xaml in the WPF Designer.
-
Name the xref:System.Windows.Controls.Grid element
grid1.[!code-xamlHostingWfInWPF#1]
-
In Design view or XAML view, select the xref:System.Windows.Window element.
-
In the Properties window, click the Events tab.
-
Double-click the xref:System.Windows.FrameworkElement.Loaded event.
-
Insert the following code to handle the xref:System.Windows.FrameworkElement.Loaded event.
[!code-csharpHostingWfInWPF#10] [!code-vbHostingWfInWPF#10]
-
At the top of the file, add the following
Importsorusingstatement.[!code-csharpHostingWfInWPF#11] [!code-vbHostingWfInWPF#11]
-
Press F5 to build and run the application.
See also
- xref:System.Windows.Forms.Integration.ElementHost
- xref:System.Windows.Forms.Integration.WindowsFormsHost
- Design XAML in Visual Studio
- Walkthrough: Hosting a Windows Forms Control in WPF by Using XAML
- Walkthrough: Hosting a Windows Forms Composite Control in WPF
- Walkthrough: Hosting a WPF Composite Control in Windows Forms
- Windows Forms Controls and Equivalent WPF Controls
- Hosting a Windows Forms Control in WPF Sample