JLott
JLott

Reputation: 1828

Add Windows User Control to a WPF View

I need to add a Windows form User Control (Barcode_Scanner.cs) to a WPF View (MainWindow.xaml)

Is there a simple way to do this? Any help would be appreciated.

Upvotes: 1

Views: 2547

Answers (2)

sa_ddam213
sa_ddam213

Reputation: 43596

You can host Windows.Forms controls using the WPF WindowsFormsHost element.

Example:

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:myControls="clr-namespace:MyContromNamespace;assembly=MyContromNamespace"
        Title="MainWindow" Height="195" Width="191" Name="UI">
    <Grid>
        <WindowsFormsHost>
            <winforms:Button Text="StackOverflow" />
        </WindowsFormsHost>

        <WindowsFormsHost>
            <myControls:MyUserControl  />
        </WindowsFormsHost>

    </Grid>
</Window>

Result:

enter image description here

Upvotes: 3

KF2
KF2

Reputation: 10143

you can do it with WindowsFormsHost

So the first thing that we need to do (after creating a new WPF project) is add a few references. You can do this by by right-clicking on the references folder in the solution explorer, and choosing "Add Reference":

enter image description here

Then you will get a dialog like this:

enter image description here

and so on wpf-tutorial-using-winforms-in-wpf

Upvotes: 1

Related Questions