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

标签 c# wpf winforms user-controls

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

另外,如果我决定只用 wpf 重写代码,会有多困难/有什么不同?

最佳答案

因为 WPF 的工作方式与 WinForms 有很大不同,所以 WPF 中没有用于 WinForms 的交互式设计器。使用WindowsFormsHost将 WinForms 控件放置在 WPF 窗口中。

您可以通过 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 功能,例如硬件加速。

关于c# - 在 WPF 工具箱中找不到我的 winforms 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29724489/

相关文章:

c# - 绑定(bind)到 bool 属性时设置 TextBlock 的默认值

Wpf DataGrid ClipboardCopyMode ="IncludeHeader"带有自定义 header

c# - ReportViewer 和辅助功能

c# - 管理内存

c# - 如何在 ListView 的 SelectionChanged 上调用 ViewModel 上的命令?

c# - Fluent API - 一对多关系

c# - 在 C# 中实现可序列化类的首选方法是什么

c# - 图表中间的图表集(0,0)

c# - 2 在 C# 中的连续游戏

c# - int + string 怎么变成字符串?