轉帖|其它|編輯:郝浩|2011-08-19 15:45:28.000|閱讀 1267 次
概述:本文主要介紹怎樣在WPF中添加Windows Form控件,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
首先,需要向項目中的reference添加兩個dll,一個是.NET庫中的System.Windows.Forms,另外一個是WindowsFormsIntegration,它的位置一般是在C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF 里。
添加完兩個dll以后,就可以在控件庫中找到WindowsFormsHost這個控件了。這個控件是我們添加Windows Form控件的基礎。跟別的其他的控件一樣,它也是可控的,可以自定義它在窗口中的位置、控件大小顏色等屬性。我一般是比較喜歡在Blend里面創建控件。可以在Blend中的Assets中找到這個控件。或者你也可以在vs中的設計模式下的toolbox中找到它。放置完以后在xmal代碼中會自動生成相應代碼:
<WindowsFormsHost Height="196" HorizontalAlignment="Left" Margin="104,65,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="286" />
然后,需要在xmal的開始處添加兩行代碼
xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration; assembly=WindowsFormsIntegration" xmlns:WinFormControls= "clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
這樣就可以在WindowsFormsHost下放置需要的Windows Form控件了,比如
<WindowsFormsHost Height="196" HorizontalAlignment="Left" Margin="104,65,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="286" > <WinFormControls:Button Text="WinformButton" Width="150"/> </WindowsFormsHost>
這是最簡單的情況,就是添加了一個button,運行以后會發現整個WindowsFormsHost上就放置了一個碩大的button……如果需要有布局的可以在WindowsFormsHost下放置Panel等布局控件。
最后附上整個xmal代碼
<Window x:Class="WpfApplication2.MainWindow"
xmlns=
"//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "//schemas.microsoft.com/winfx/2006/xaml"
xmlns:WinFormHost=
"clr-namespace:System.Windows.Forms.Integration;
assembly=WindowsFormsIntegration"
xmlns:WinFormControls=
"clr-namespace:System.Windows.Forms;assembly=
System.Windows.Forms"
Title= "MainWindow" Height="350" Width="525">
<Grid>
<WindowsFormsHost Height="196"
HorizontalAlignment="Left" Margin="104,65,0,0"
Name="windowsFormsHost1"
VerticalAlignment="Top" Width="286" >
<WinFormControls:Button Text="WinformButton" Width="150"/>
</WindowsFormsHost>
</Grid>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園