WPF调用Winform控件具体技巧

WPF开发工具可以帮助我们实现许多功能。对于开发者而言,这是一个具有重要意义的开发工具。我们在这里就为大家介绍一下有关WPF调用Winform控件的相关实现方法。

 

 

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

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

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

 
 
  1. < Window x:Class="CrossBowDemo.
    MainWindow"
     
  2. xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" 
  3. xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  6. Title="Hosting Windows Forms Control In WPF"   
  7. Height="300"   Width="650"   
  8. ResizeMode="NoResize" Loaded="WindowLoadedHandler"   > 
  9. < /Window> 

 

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

原文添加的是 DataGridView控件:

 

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

本人添加的是 NumericUpDown控件:

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

 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值