简体   繁体   English

在WinForms中使用自定义WPF控件

[英]Using a custom WPF control in WinForms

I'm going to have to build a custom control for a WinForms application. 我将不得不为WinForms应用程序构建自定义控件。 But in the near future that same control will have to be used in a WPF application. 但在不久的将来,必须在WPF应用程序中使用相同的控件。

I've already written all the logic in a controller class which I can use for both WinForms and WPF (I will only have some databinding to do and that's it), but of course it would be easier to develop and maintain the control only once. 我已经在控制器类中编写了所有逻辑,我可以将它用于WinForms和WPF(我只会有一些数据绑定来做,就是这样),但当然只开发和维护控件一次会更容易。

So can I build the control in WPF and then put them on a WinForms form? 那么我可以在WPF中构建控件然后将它们放在WinForms表单上吗? My projects currently have 3.0 as their target framework. 我的项目目前有3.0作为目标框架。

Yes you can! 是的你可以!

In WinForms use ElementHost and add you WPF control inside (Best practice here is to create a WPF User Control and add you controls into the user control) 在WinForms中使用ElementHost并在其中添加WPF控件(这里的最佳实践是创建WPF用户控件并将控件添加到用户控件中)

And in WPF, if you want to use a WinForms user control, use the WindowsFormsHost 在WPF中,如果要使用WinForms用户控件,请使用WindowsFormsHost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM