How to set user control of Windows forms in a WPF application?

As a short-term solution, I am trying to hush up the "usercontrol" form in a WPF application. I see in the WPF application view that I can add a “custom Windows form control” to the project, and it creates an empty custom control, but I cannot figure out how to add it. Ideally, I would like to know how to take a .dll from my compiled Windows Forms user element and paste it into a WPF application or import a user control into a WPF application.

Thanks Sam

+5
source share
3 answers

, Windows Forms. "host" WPF.

, MSDN.

( ):

<Window x:Class="Window1"
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="HostingWfInWpf">
<Grid>
    <WindowsFormsHost>
       <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
    </WindowsFormsHost>
</Grid>
</Window>
+7

System.Windows.Forms WindowsFormsIntegration

xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:WindowsFormsIntegration="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

Windows- .

  <WindowsFormsHost Name="wfhDate"  
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Stretch">
                <WinForms:FlowLayoutPanel/>
  </WindowsFormsHost>

#

using Forms = System.Windows.Forms;
.........................
Forms.FlowLayoutPanel flpPanel = this.wfhDate.Child as Forms.FlowLayoutPanel;
// Initialize your Forms contol here.
flpPanel.Controls.Add( yourControl );
+3

Lucas answer is correct, but I wanted to add something necessary. If you are creating a web application, you must change the Security setting to This is a full trust application. I could not get the WindowsFormsHost control to work before that.

0
source

All Articles