Add a Windows Forms Control in a WPF Application

By Michael Detras

To add a windows forms control in WPF, you must use a WindowsFormsHost.

In your XAML (or designer), add a WindowsFormsHost control. To add a Windows Forms control, add a reference to the System.Windows.Forms DLL to your WPF project, add the XML namespace for the Windows Forms namespace, then add the control inside the WindowsFormsHost, like the one below.

<Window x:Class="WpfApplication1.MainWindow"
         
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        
Title="MainWindow" Height="350" Width="525">
    <Grid Background="LightGray">
        <WindowsFormsHost Height="23" HorizontalAlignment="Left" Margin="12,22,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="250">
            <wf:Button x:Name="btn" Text="Click Me!" Click="Button_Click"/>
        </WindowsFormsHost>
    </Grid>
</Window>

You can access the button like when you are programming Windows Forms.

public partial class MainWindow : Window
{
    public MainWindow()
    {
         InitializeComponent();
    }

     private void Button_Click(object sender, EventArgs e)
    {
         btn.Visible = false;
    }
}

Add a Windows Forms Control in a WPF Application  (1294 Views)