[英]Using part of a large WinForms project inside a large WPF project
這兩個應用程序都相當陳舊,並且由幾個人構建和維護了幾年。 目前,WinForms項目中使用的控件之一確實需要在WPF項目中顯示。
我已經閱讀過在WPF項目中使用WinForms控件,並且在大多數情況下如果你只是實例化一個常規的空WinForm控件,它似乎相對簡單。
我想知道你最好如何在另一個項目中使用大型項目的一部分? 理想情況下,WinForm控件將在我們的一個WPF控件中,在ONE選項卡上,在發送並加載所需數據后可見。
以下是一些一般性指南。
從WPF應用程序中,將項目引用添加到:
WindowsFormsIntegration
System.Windows.Forms
修改您的XAML以包含WindowsFormsHost
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabItem Header="Old Form">
<WindowsFormsHost Name="WinFormsHost"></WindowsFormsHost>
</TabItem>
</TabControl>
</Grid>
</Window>
實例化舊的Form
並將其設置為WindowsFormsHost
的子級。 將TopLevel
設置為false
或者它會抱怨“子控件不能是頂級窗體”。 也可以更改FormBorderStyle
,以防止表單的標題欄顯示並允許用戶拖動表單。
public MainWindow()
{
InitializeComponent();
WinFormsHost.Child =
new Form1 { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
}
你最終得到這樣的東西:
您可以在“ 演練:在WPF中托管Windows窗體控件 ”和“ WindowsFormsHost類 ”的MSDN文檔中閱讀更多內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.