简体   繁体   中英

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.

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:

在此输入图像描述

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":

在此输入图像描述

Then you will get a dialog like this:

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM