我从一个项目中制作了自定义winforms控件,然后切换到WPF。我引用了旧项目,但我的自定义控件并没有显示在工具箱中,而且似乎没有#34; easy"将它们添加到我的winforms主机的方法。我是否必须在代码中添加它们?
另外,如果我决定在wpf中重新编写代码,它会有多难/不同?
答案 0 :(得分:3)
因为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功能,如硬件加速。