WPF調(diào)用Winform控件具體技巧分享
WPF開發(fā)工具可以幫助我們實(shí)現(xiàn)許多功能。對于開發(fā)者而言,這是一個(gè)具有重要意義的開發(fā)工具。我們在這里就為大家介紹一下有關(guān)WPF調(diào)用Winform控件的相關(guān)實(shí)現(xiàn)方法。#t#
WPF調(diào)用Winform控件實(shí)現(xiàn)主要分三步:
WPF調(diào)用Winform控件1、添加兩個(gè)引用:WindowsFormsIntegration.dll (負(fù)責(zé)整合WPF和Windows)、System.Windows.Forms.
WPF調(diào)用Winform控件2、在 XAML文件中添加兩個(gè)引用(粗體部分):
- < Window x:Class="CrossBowDemo.
MainWindow"- xmlns:wfi ="clr-namespace:System.
Windows.Forms.Integration;assembly
=WindowsFormsIntegration"- xmlns:wf ="clr-namespace:System.
Windows.Forms;assembly=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>
WPF調(diào)用Winform控件3、在XAML編碼區(qū)實(shí)現(xiàn)你想添加的控件:
原文添加的是 DataGridView控件:
- < wfi:WindowsFormsHost>
- < !-- Set some properties on
Windows Forms control in Xaml -->- < wf:DataGridView x:Name=
"dataGridView" Dock="Fill"
SelectionMode="FullRowSelect"/>- < /wfi:WindowsFormsHost>
本人添加的是 NumericUpDown控件:
- < 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>
在本人的WPF調(diào)用Winform控件代碼中Grid的作用相當(dāng)于Web頁面中用來布局的Table。 此處加上Grid是為了方便移動(dòng)控件的位置。