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

WPF调用Winform控件

1、添加两个引用:

WindowsFormsIntegration(负责整合WPF和Windows)
System.Windows.Forms

2、在 XAML文件中添加两个引用:

1
2
xmlns:wfi = "clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf = "clr-namespace:System.Windows.Forms; assembly = System.Windows.Forms"

3、在XAML编码区实现你想添加的控件:

添加DataGridView控件:

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

添加 NumericUpDown控件:

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