使用自定义WPF控件中的WinForms自定义、控件、WPF、WinForms

2023-09-02 01:35:30 作者:老衲用洗头用妇炎洁

我将不得不建立一个WinForms应用程序的自定义控制。但在不久的将来同样的控制将在WPF应用程序中使用。

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.

所以,我可以建立在W​​PF控件,然后把它们放在一个WinForms形式? 我的项目目前有3.0作为其目标框架。

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.

推荐答案

当然可以!

在使用的WinForms ElementHost的并添加WPF内部控制(这里最好的做法是创建一个WPF用户控件并添加控件到用户控件)

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)

和WPF中,如果你想使用的WinForms用户控件,使用的WindowsFormsHost

And in WPF, if you want to use a WinForms user control, use the WindowsFormsHost

WPF互操作:空域和窗口区域概述 Windows窗体和WPF属性映射 如何:使用ElementHost的主机在Windows窗体在Windows presentation基金会控制 演练:托管Windows窗体控件在Windows presentation基金会 WPF Interoperation: "Airspace" and Window Regions Overview Windows Forms and WPF Property Mapping How to: Host a Windows Presentation Foundation Control in Windows Forms by Using ElementHost Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation