1、      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll。

2、      在要使用WinForm控件的WPF窗体的XAML文件中添加如下内容:

即:

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

3、       在WPF的容器控件内如StackPanel内首先要添加WinForm控件的宿主容器,用于衔接WPF和WinForm,

对应XAML如下:

 <StackPanel>
<wfi:WindowsFormsHost>
<wf:Label x:Name="wfLabel" Text="winForm控件在此" />
</wfi:WindowsFormsHost> <wfi:WindowsFormsHost>
<wf:Button x:Name="wfButton" Text="确定" Click="wfButton_Click" />
</wfi:WindowsFormsHost> <Button Content="Button" Margin="10" Name="button1"/>
</StackPanel>

说明:<wfi:WindowsFormsHost></wfi:WindowsFormsHost>即为WinForm控件的宿主容器,每一个宿主容器只能放一个WinForm控件,如下例,放了三个WinForm控件,分别放在三个宿主容器里面,该容器可以设置属性来调整大小和布局。

注意:如上我添加的WinForm控件如在指定其Name时,必须加前缀x:,如添加Lable时<wf:Label x:Name="wpfLabel" Text="我是WPF中的WinForm控件” />,否则后台代码无法访问。

4、       如果要在WPF后台代码中访问上面的Lable,可直接像在WinForm中使用一样。如在点击某一按钮时改变Lable内容,代码如下:wpfLabel.Text=”内容已改变”;

5、       以使用WinForm控件wpfLabel改变标记为例,说明后台用法:

完整后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes; namespace ChangeDetection
{
/// <summary>
/// Window4.xaml 的交互逻辑
/// </summary>
public partial class Window4 : Window
{
public Window4()
{
InitializeComponent();
} private void wfButton_Click(object sender, EventArgs e)
{
wfLabel.Text= "内容已改变"; }
}
}

完整XAML如下:

<Window x:Class="ChangeDetection.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ChangeDetection"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
mc:Ignorable="d"
Title="Window4" Height="300" Width="300"> <StackPanel>
<wfi:WindowsFormsHost>
<wf:Label x:Name="wfLabel" Text="winForm控件在此" />
</wfi:WindowsFormsHost> <wfi:WindowsFormsHost>
<wf:Button x:Name="wfButton" Text="确定" Click="wfButton_Click" />
</wfi:WindowsFormsHost> <Button Content="Button" Margin="10" Name="button1"/>
</StackPanel> </Window>

在WPF中使用WinForm控件方法的更多相关文章

  1. [转]在WPF中使用WinForm控件方法

    本文转自:http://blog.csdn.net/lianchangshuai/article/details/6415241 下面以在Wpf中添加ZedGraph(用于创建任意数据的二维线型.条型 ...

  2. 在WPF中调用Winform控件

    最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...

  3. wpf中插入winform控件并获取句柄

    因工作需要使用wpf做界面,而有个开发包依赖picturebox控件,上网研究了一下,总算弄通了. 首先在项目中添加引用System.Windows.Forms与WindowsFormsIntegra ...

  4. 如何在WPF中调用Winform控件

    原文地址:http://hi.baidu.com/stuoopluwqbbeod/item/32ec38403da42ee2bcf45167 功能实现主要分三步:1.添加两个引用:WindowsFor ...

  5. Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决

    有人会说不建议Wpf中使用Winform控件,有人会说建议使用Winform控件在Wpf下的替代方案,然而在实际工作中由于项目的特殊需求,考虑到时间.成本等因素,往往难免会碰到在WPF中使用Winfr ...

  6. WPF中的image控件的Source赋值

    WPF中的Image控件Source的设置 1.XAML中 简单的方式(Source="haha.png"); image控件的Source设置为相对路径后(Source=&quo ...

  7. WPF中的ControlTemplate(控件模板)(转)

    原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html WPF中的ControlTemplate(控件模板)     ...

  8. WPF保存包含Winform控件的XAML页面问题

    原文:WPF保存包含Winform控件的XAML页面问题 最近的工作中,用到了WPF调用Winform控件 但是在保存XAML页面的时候发现了问题,就是Winform页面黑黑的,没有任何渲染的波形曲线 ...

  9. WPF中的ControlTemplate(控件模板)

    原文:WPF中的ControlTemplate(控件模板) WPF中的ControlTemplate(控件模板)                                             ...

随机推荐

  1. js-读取复选框

    js: var obj = document.getElementsByName("yk"); var check_val = []; for(k in obj){ if(obj[ ...

  2. 【hrbust2293】棋盘村

    题意 哈理工2016级新生程序设计全国邀请赛A题 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&probl ...

  3. Python matplotlib笔记

    可视化的工具有很多,如Tableau,各种JS框架,我个人感觉应该是学JS最好,因为JS不需要环境,每个电脑都有浏览器,而像matplotlib需要Python这样的开发环境,还是比较麻烦的,但是毕竟 ...

  4. jQuery—常用操作

    一.jquery各版本变化 1.3:增加live(),为当前和将来增加的元素绑定事件 1.4:增加delegate().undelegate(),替代live(),可以遍历绑定 1.6:2个破坏性变更 ...

  5. JS 中如何将<br/> 替换成 /n

    JS 中如何将<br/> 替换成 /n function a() { var data = "aaaa<br/>bbbb<br/>cccc"; ...

  6. 谈iOS抓包:Mac下好用的HTTP/HTTPS抓包工具Charles

    在Mac下做开发,用Fiddler抓包由于离不开Windows比较痛苦,还好有Charles,到官网http://www.charlesproxy.com/可下载到最新版本(若不支持rMBP可拖到Re ...

  7. HTML5学习总结-番外05 移动终端适配

    一 viewport 在使用移动端设备浏览网页时,移动端浏览器是直接把整个页面放到一个虚拟的视图里来显示的,通常来说这个虚拟的视图大小会比手机屏幕大,用户可以通过手势操作来平移.缩放这个视图. 如果不 ...

  8. 搭建NFS服务器

    1:yum install -y nfs-utils-* portmap-* 2:NFS安装完毕,需要创建共享目录,共享目录在vi /etc/exports文件里面配置,可配置参数如下: /data/ ...

  9. Java直接内存与堆内存

    NIO的Buffer提供了一个可以不经过JVM内存直接访问系统物理内存的类——DirectBuffer. DirectBuffer类继承自ByteBuffer,但和普通的ByteBuffer不同,普通 ...

  10. 一道Apple公司(中国)的面试题目

    Apple在中国(上海)有公司业务,但是感觉主要是做测试工作的部门,主要是保障Apple的产品质量QE.面试的时候,面试官出了一道题目,我貌似曾今开过类似的题目,但是由于当场发挥不佳没有答出来.题目大 ...