在WinForms中使用自定义WPF控件

时间:2021-10-04 01:21:27

I'm going to have to build a custom control for a WinForms application. But in the near future that same control will have to be used in a WPF application.

我将不得不为WinForms应用程序构建自定义控件。但在不久的将来,必须在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? My projects currently have 3.0 as their target framework.

那么我可以在WPF中构建控件然后将它们放在WinForms表单上吗?我的项目目前有3.0作为目标框架。

1 个解决方案

#1


17  

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

#1


17  

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