2013-04-05 46 views
1

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

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

+0

是你的用户控件中的WinForms做?你想将它添加到WPF窗口? – Dilshod 2013-04-05 02:48:00

+0

只需将它放在一个'WindowsFormsHost'控件中(它在您的工具箱中) – 2013-04-05 02:51:27

回答

3

您可以使用WPF WindowsFormsHost元素来控制Windows.Forms控件。

例子:

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

结果:

enter image description here

+0

我怎样才能添加自定义控件?一个我创造的。 – JLott 2013-04-05 21:04:20

+0

只需为您的自定义控件名称空间设置一个'xmlns'定义,添加示例以回答。 – 2013-04-05 22:52:01

1

你可以用WindowsFormsHost

做到这样我们需要做的(创建一个新的WPF项目之后)的第一件事是添加一些引用。您可以通过在Solution Explorer中的引用文件夹中右击做到这一点,并选择“添加引用”:

enter image description here

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

enter image description here

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

+0

您通常不需要手动添加引用,在VS2010和VS2012中,当您将'System.Windows.Forms'引用添加到' 'WPF'元素上的WindowsFormsHost' – 2013-04-05 03:10:18

+0

@ sa_ddam213:是的,我只是想详细说明:D – KF2 2013-04-05 03:12:49