itlaoli

WPF调用Winform控件技巧

WPF调用Winform控件实现主要分三步:

WPF调用Winform控件1、添加两个引用:WindowsFormsIntegration.dll (负责整合WPF和Windows)、System.Windows.Forms.

WPF调用Winform控件2、在 XAML文件中添加两个引用(粗体部分):

 1 < Window x:Class="CrossBowDemo.
 2 MainWindow" 
 3 
 4 xmlns:wfi ="clr-namespace:System.
 5 Windows.Forms.Integration;assembly
 6 =WindowsFormsIntegration" 
 7 
 8 xmlns:wf ="clr-namespace:System.
 9 Windows.Forms;assembly=System.
10 Windows.Forms" 
11 
12 xmlns="http://schemas.microsoft.com/
13 winfx/2006/xaml/presentation" 
14 
15 xmlns:x="http://schemas.microsoft.
16 com/winfx/2006/xaml" 
17 
18 Title="Hosting Windows Forms 
19 Control In WPF"   
20 
21 Height="300"   
22 
23 Width="650"   
24 
25 ResizeMode="NoResize" 
26 
27 Loaded="WindowLoadedHandler"   
28 
29 > 
30 
31 < /Window> 
View Code

WPF调用Winform控件3、在XAML编码区实现你想添加的控件

原文添加的是 DataGridView控件:

 1 < wfi:WindowsFormsHost> 
 2 
 3 < !-- Set some properties on 
 4 Windows Forms control in Xaml --> 
 5 
 6 < wf:DataGridView x:Name=
 7 "dataGridView" Dock="Fill" 
 8 SelectionMode="FullRowSelect"/> 
 9 
10 < /wfi:WindowsFormsHost> 
View Code

 

本人添加的是 NumericUpDown控件:

 1 < Grid Height="0" Margin=
 2 "146,0,0,116" MinHeight="20" 
 3 MinWidth="20" Name="grid1" 
 4 VerticalAlignment="Bottom" 
 5 HorizontalAlignment="Left" 
 6 Width="50"> 
 7 
 8 < wfi:WindowsFormsHost> 
 9 
10 < wf:NumericUpDown x:Name=
11 "nupCounter" Maximum="100">
12 < /wf:NumericUpDown> 
13 
14 < /wfi:WindowsFormsHost> 
15 
16 < /Grid> 
View Code

在本人的WPF调用Winform控件代码中Grid的作用相当于Web页面中用来布局的Table。 此处加上Grid是为了方便移动控件的位置。

posted @ 2013-07-31 18:49  竹--石  阅读(233)  评论(0编辑  收藏  举报