In my wpf application i am trying to use windows form control…. but i am getting an error i.e
Error The type 'WindowsFormsHost' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
can any one help me to get it done… the following is my code
c#:code
public Window2()
{
InitializeComponent();
System.Windows.Forms.PictureBox PictureBox1 = new System.Windows.Forms.PictureBox();
windowsFormsHost1.Child = PictureBox1;
PictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(PictureBox1_Paint);
}
xaml:code
<WindowsFormsHost Height="175" HorizontalAlignment="Left" Margin="10,10,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="255" />
Best Answer
Normally when we see an error that says:
There could be a few problems. Either that means that we didn't add a reference of the relevant dll to our project, or we didn't import the namespace properly.
To find which dll we need to add a reference to, we normally go to MSDN, searching with the search terms '
SomeClass
class'... in your case 'WindowsFormsHost
class'. Doing this for theWindowsFormsHost
class, we see this:Note the line that says:
Looking in the Add Reference dialog in Visual Studio, we can see a dll entry for
WindowsFormsIntegration
:So that's how we find out which dll to import. Now, we just need to make sure that we import the namespace properly, either in C#:
In XAML, you do not need to add an XML Namespace for the
WindowsFormsIntegration
dll before using theWindowsFormsHost
control.See the
WindowsFormsHost
Class page on MSDN for more information.