如何在WPF应用程序中放置自定义Windows窗体控件?

时间:2021-01-07 20:37:19

As a short term solution I'm trying to jam a windows form 'usercontrol' into a WPF application. I see in the WPF application view that I can add a 'custom windows form control' to the project and it makes an empty custom control, but I can't figure out how to add it. Ideally I'd like to know how to take the .dll from my compiled windows forms user control and stick it into the WPF app, or import the user control into the WPF application.

作为一个短期解决方案,我试图将Windows窗体'usercontrol'堵塞到WPF应用程序中。我在WPF应用程序视图中看到我可以向项目添加“自定义窗体控件”,它会创建一个空的自定义控件,但我无法弄清楚如何添加它。理想情况下,我想知道如何从我编译的Windows窗体用户控件中获取.dll并将其粘贴到WPF应用程序中,或将用户控件导入WPF应用程序。

Thanks, Sam

3 个解决方案

#1


7  

You can't really add it as a control to the toolbox like you could for a Windows Forms Application. What you should do instead is "host" the user control inside of the WPF application.

您无法像在Windows窗体应用程序中那样将其作为控件添加到工具箱中。你应该做的是“托管”WPF应用程序内部的用户控件。

See how to do it on MSDN.

了解如何在MSDN上执行此操作。

Here's an example of how to use a masked text box (which you can easily modify to use your custom control):

以下是如何使用屏蔽文本框(您可以轻松修改以使用自定义控件)的示例:

<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:System.Windows.Forms;assembly=System.Windows.Forms"  
Title="HostingWfInWpf">
<Grid>
    <WindowsFormsHost>
       <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
    </WindowsFormsHost>
</Grid>
</Window>

#2


3  

Add a reference to System.Windows.Forms and WindowsFormsIntegration to your Project

将System.Windows.Forms和WindowsFormsIntegration的引用添加到项目中

xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:WindowsFormsIntegration="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

And place Windows forms host in the window.

并将Windows窗体主机放在窗口中。

  <WindowsFormsHost Name="wfhDate"  
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Stretch">
                <WinForms:FlowLayoutPanel/>
  </WindowsFormsHost>

Now in C# code

现在用C#代码

using Forms = System.Windows.Forms;
.........................
Forms.FlowLayoutPanel flpPanel = this.wfhDate.Child as Forms.FlowLayoutPanel;
// Initialize your Forms contol here.
flpPanel.Controls.Add( yourControl );

#3


0  

Lucas' answer is correct, but I wanted to add something needed. If you are creating a web application, then you must change the Security setting to This is a full trust application. I could not get the WindowsFormsHost control to work prior to doing this.

卢卡斯的回答是正确的,但我想补充一些必要的东西。如果要创建Web应用程序,则必须将“安全性”设置更改为“这是完全信任的应用程序”。在执行此操作之前,我无法使WindowsFormsHost控件正常工作。

#1


7  

You can't really add it as a control to the toolbox like you could for a Windows Forms Application. What you should do instead is "host" the user control inside of the WPF application.

您无法像在Windows窗体应用程序中那样将其作为控件添加到工具箱中。你应该做的是“托管”WPF应用程序内部的用户控件。

See how to do it on MSDN.

了解如何在MSDN上执行此操作。

Here's an example of how to use a masked text box (which you can easily modify to use your custom control):

以下是如何使用屏蔽文本框(您可以轻松修改以使用自定义控件)的示例:

<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:System.Windows.Forms;assembly=System.Windows.Forms"  
Title="HostingWfInWpf">
<Grid>
    <WindowsFormsHost>
       <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
    </WindowsFormsHost>
</Grid>
</Window>

#2


3  

Add a reference to System.Windows.Forms and WindowsFormsIntegration to your Project

将System.Windows.Forms和WindowsFormsIntegration的引用添加到项目中

xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:WindowsFormsIntegration="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

And place Windows forms host in the window.

并将Windows窗体主机放在窗口中。

  <WindowsFormsHost Name="wfhDate"  
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Stretch">
                <WinForms:FlowLayoutPanel/>
  </WindowsFormsHost>

Now in C# code

现在用C#代码

using Forms = System.Windows.Forms;
.........................
Forms.FlowLayoutPanel flpPanel = this.wfhDate.Child as Forms.FlowLayoutPanel;
// Initialize your Forms contol here.
flpPanel.Controls.Add( yourControl );

#3


0  

Lucas' answer is correct, but I wanted to add something needed. If you are creating a web application, then you must change the Security setting to This is a full trust application. I could not get the WindowsFormsHost control to work prior to doing this.

卢卡斯的回答是正确的,但我想补充一些必要的东西。如果要创建Web应用程序,则必须将“安全性”设置更改为“这是完全信任的应用程序”。在执行此操作之前,我无法使WindowsFormsHost控件正常工作。