views:

1504

answers:

2

As a short term solution I'm trying to jam a windows form 'usercontrol' into a WPF application. I see in the WPF application view that I can add a 'custom windows form control' to the project and it makes an empty custom control, but I can't figure out how to add it. Ideally I'd like to know how to take the .dll from my compiled windows forms user control and stick it into the WPF app, or import the user control into the WPF application.

Thanks, Sam

+3  A: 

You can't really add it as a control to the toolbox like you could for a Windows Forms Application. What you should do instead is "host" the user control inside of the WPF application.

See how to do it on MSDN.

Here's an example of how to use a masked text box (which you can easily modify to use your custom control):

<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>
Lucas McCoy
I tried this code exactly as is as well and got the same error I mention above.
Shizam
Did you add a reference to the `System.Windows.Forms` assembly in your References folder?
Lucas McCoy
Yup, added them to the references folder. I looked around some and came back to the page you linked, then found the page above it shows you how to add a WF control using c# instead of XAML. I tried that and it worked, so for whatever reason when doing a WPF Web Application I can't get it to work via XAML but I can via c#.http://msdn.microsoft.com/en-us/library/ms751761.aspx
Shizam
Marking this guy as the answer as it led me to the solution:http://msdn.microsoft.com/en-us/library/ms751761.aspx
Shizam
Awesome, this fully worked as intended. I was able to take the resulting dll file from my windows form user control and add it as a reference then add it to the WindowsFormsHost. Lovely.
Shizam
Glad I could help. Thanks!
Lucas McCoy
+1  A: 

Add a reference to System.Windows.Forms and WindowsFormsIntegration to your Project

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

And place Windows forms host in the window.

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

Now in C# code

using Forms = System.Windows.Forms;
.........................
Forms.FlowLayoutPanel flpPanel = this.wfhDate.Child as Forms.FlowLayoutPanel;
// Initialize your Forms contol here.
flpPanel.Controls.Add( yourControl );
Sauron
@Lucas: I copied the XAML from above exactly and it compiled without error, I didn't put in the C# code yet as I just wanted to make sure the XAML worked. I ran the app and it crashed with this exception:System.Windows.Markup.XamlParseException was unhandledMessage: Cannot create instance of 'Page1' defined in assembly 'WpfUploaderTwo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'Page1.xaml' Line 1 Position 7.
Shizam
If I take out the WindowsFormsHost bit, it works just fine. Link to code and error: http://bit.ly/aGqfG
Shizam
Ah, so I'm making a 'WPF Web Application' not a 'WPF Application', not sure if that has any bearing.
Shizam