1

我需要将 Windows 窗体用户控件 (Barcode_Scanner.cs) 添加到 WPF 视图 (MainWindow.xaml)

有没有一种简单的方法可以做到这一点?任何帮助,将不胜感激。

4

2 回答 2

3

您可以使用 WPF元素承载Windows.Forms控件。WindowsFormsHost

例子:

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

结果:

在此处输入图像描述

于 2013-04-05T02:55:45.143 回答
1

你可以用WindowsFormsHost

所以我们需要做的第一件事(在创建一个新的 WPF 项目之后)是添加一些引用。您可以通过右键单击解决方案资源管理器中的引用文件夹并选择“添加引用”来执行此操作:

在此处输入图像描述

然后你会得到一个这样的对话框:

在此处输入图像描述

等等 wpf-tutorial-using-winforms-in-wpf

于 2013-04-05T02:54:48.310 回答