在 WPF 工具箱中找不到我的 winforms 控件

Can't find my winforms control in WPF toolbox

我从一个项目中制作了自定义 winforms 控件,然后切换到 WPF。我引用了旧项目,但我的自定义控件没有出现在工具箱中,而且似乎没有 "easy" 方法可以将它们添加到我的 winforms 主机。我必须在代码中添加它们吗?

此外,如果我决定只重写 wpf 中的代码,difficult/different 会怎样?

由于 WPF 的工作方式与 WinForms 大不相同,因此 WPF 中没有用于 WinForms 的交互式设计器。使用 WindowsFormsHost 将 WinForms 控件放入 WPF window.

您可以通过 XAML 执行此操作:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:My.Custom.Assembly;assembly=My.Custom.Assembly"  
Title="HostingWfInWpf">
<Grid>
    <WindowsFormsHost>
       <wf:MyCustomControl />
    </WindowsFormsHost>
</Grid>
</Window>

My.Custom.Assembly 替换为您自己的程序集,并将 MyCustomControl 替换为您的控件名称。

如果可能,为 WPF 重写控件将是最佳选择,因为它将允许硬件加速等 WPF 功能。