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

标签 c# wpf winforms user-controls

我需要将 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>

结果:

enter image description here

关于c# - 将 Windows 用户控件添加到 WPF View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15824994/

相关文章:

wpf - 在客户端计算机上调试 WPF

wpf - 仅当某些条件为真时,通过 Binding 将 TextBlock 设为粗体

c# - 如何从MVVMCross中的另一个ViewModel捕获事件

c# - 从两个文本框中添加值并在第三个文本框中显示总和

c# - 为什么我的 WinForm 控件在绘制事件之前会闪烁?

c# - 如何将泛型类型导出到 COM?类型库导出器在签名中遇到泛型类型实例

c# - ViewModel 不在数据网格中显示元素

c# - 我的 WPF 应用程序未触发 MainWindow_Loaded

.net - 如何在 VB.NET 中创建命令链接按钮(具有多行文本)?

c# - 如何测试给定的功能代码单元 (C#) 是否不创建/写入任何文件到磁盘?