Skip to main content
 首页 » 编程设计

c#之将 Windows 用户控件添加到 WPF View

2024年02月13日13kenshinobiy

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

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

请您参考如下方法:

您可以使用 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> 

结果: