
1、添加两个引用:WindowsFormsIntegration.dll(负责整合WPF和Windows)、System.Windows.Forms.
2、在 XAML文件中添加两个引用(粗体部分):
<Window x:Class="CrossBowDemo.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integrationassembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Formsassembly=System.Windows.Forms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
title="Hosting Windows Forms Control In WPF"
Height="300"
Width="650"
ResizeMode="NoResize"
Loaded="WindowLoadedHandler"
>
</Window>
3、在XAML编码区实现你想添加的控件:
原文添加的是DataGridView控<wfi:WindowsFormsHost>
<wf:DataGridView x:Name="Dg" Dock="Fill" SelectionMode="FullRowSelect">
</wf:DataGridView>
</wfi:WindowsFormsHost>件:
使用方法:1、首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration;
2、添加完两个动态dll以后,就可以在控件库中找到WindowsFormsHost这个控件;
3、将这个控件放入窗体,放置完以后在xmal代码中会自动生成相应代码:
<Grid>
<WindowsFormsHost Height="100" HorizontalAlignment="Left" Margin="36,29,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="200" />
</Grid>
4、然后,需要在xmal的开始处添加两行代码 :
xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integrationassembly=WindowsFormsIntegration"
xmlns:WinFormControls="clr-namespace:System.Windows.Formsassembly=System.Windows.Forms"
5、这样就可以在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>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)