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. 分享“12306 P2P平台”创业Idea

      结合云平台抢票,社区,P2P等性质,实施供求抢票平台,能有效提高和整合抢票市场,抢票优势以杜绝黄牛,给散户提供更有利的抢票途径.本在11月计划实施,后去搞比特币,故分享摘要集思广益.     背景 ...

  2. DDR相关的低功耗技术之PASR、TCSR、DPD

    随着智能机的发展,DDR内存容量越来越大,bank数量越来越多,功耗也越来越大.在不需要的时候关闭部分bank,或者降低自刷新频率,或者进入深度低功耗模式.有三种DDR技术用来降低功耗: PASR(P ...

  3. ERwin入门

    1.菜单介绍 2.选择建模语言符号

  4. 分享一些平时测试用的sql payloads

    1:BOOL SQLINJECTION \'"%df'%df" and 1=1 and 1=2' and '1'='1' and '1'='2" and "1& ...

  5. 纯JS判断各种浏览器类型及版本.

    IE11或者非IE if (!document.all) { alert('IE11+ or not IE'); } IE10 if (document.all && document ...

  6. MVC学习随笔----如何在页面中添加JS和CSS文件

    http://blog.csdn.net/xxjoy_777/article/details/39050011 1.如何在页面中添加Js和CSS文件. 我们只需要在模板页中添加JS和CSS文件,然后子 ...

  7. Visual Studio 2010 下 安装RGiesecke.DllExport

    RGiesecke.DllExport 在 UnmanagedExports 中.安装过程如下: 1.首先在"工具"菜单下的"扩展管理器"中,安装 NuGet ...

  8. 脚手架搭建的vue项目里引入jquery和bootstrap

    引入jquery: 1.在cmd输入:npm install jquery,回车,等待.. 2.在webpack.base.conf.js里进行如下操作: 3.在webpack.prod.conf.j ...

  9. Sed、Awk单行脚本快速参考

    文本间隔: # 在每一行后面增加一空行 sed G awk '{printf("%s\n\n",$0)}' # 将原来的所有空行删除并在每一行后面增加一空行. # 这样在输出的文本 ...

  10. iOS 组件化方案探索

    来自bang's blog http://blog.cnbang.net/tech/3080/