在WPF中使用WinForm控件方法
原文:在WPF中使用WinForm控件方法

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>

posted on 2018-07-12 18:28 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/9301041.html

在WPF中使用WinForm控件方法相关推荐

  1. 如何在wpf中使用winform控件或者winform的自定义控件

    前言 在wpf中使用winform控件或者winform的自定义控件 一.添加引用 WindowsFormsIntegration.dll System.Windows.Forms.dll 提示:这两 ...

  2. WPF中使用Winform控件

    在项目中遇到使用WPF做上位机,引用Winform控件,特此做一下总结: 1.在设计界面添加: xmlns:wf="clr-namespace:System.Windows.Forms;as ...

  3. 如何在WPF中使用Winform控件

    要在WPF中使用WInform组件,必须将WInform组件放在宿主WindowsFormsHost中. WindowsFormsHost是WPF的一个控件,它允许在WPF应用程序中托管Windows ...

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

    功能实现主要分三步: 1.添加两个引用:WindowsFormsIntegration.dll (负责整合WPF和Windows).System.Windows.Forms. 2.在 XAML文件中添 ...

  5. winform界面嵌入dwg图纸_WPF中使用WinForm控件预览DWG文件(学习笔记)

    操作环境:XP,C# ,.Net4.0和VS2010中测试 WinForm中使用DWGThumbnail不用这么麻烦,下面讨论的是在WPF中使用,WPF中无法直接引用DWGThumbnail.ocx来 ...

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

    Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决 参考文章: (1)Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决 (2)https://www. ...

  7. html 获取控件位置,html控件_获取HTML中的父控件方法

    摘要 腾兴网为您分享:获取HTML中的父控件方法,月星家居,优衣库,易视云3,悟空单车等软件知识,以及vsco安卓,墨墨背单词,八一军婚网,英语打字软件,地球仪,游戏音效制作,launcher.exe ...

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

    最近的工作中,用到了WPF调用Winform控件 但是在保存XAML页面的时候发现了问题,就是Winform页面黑黑的,没有任何渲染的波形曲线. 查了原因,大概的意思是指渲染的方式不一样,所以会有这个 ...

  9. WPF中使用浏览器控件WebBrowser

    设置使用IE的版本 public static class Extensions{#region 设置WebBroswer 使用IE版本public static void SetWebBrowser ...

最新文章

  1. efcore mysql autofac_Asp.NetCore3.1版本的CodeFirst与经典的三层架构与AutoFac批量注入
  2. 014 怪物过滤的设计和实现
  3. mynewt 编译环境搭建
  4. java iterator获取索引_2020年Java面试题最新整理(1625)
  5. python能做机器人吗_python深度学习 人工智能是做机器人吗?
  6. 计算机信息安全基础薄弱具体,信息安全工程师易错题精讲十三
  7. 微信小程序怎么绑定服务器,微信小程序页面表单如何跟图片一起上传服务器
  8. php正则表达式2,php正则表达式基本语法(2)
  9. 【测试】软件测试用例设计
  10. idea 优化_JVM优化:如何利用VisualVM对高并发项目进行性能分析
  11. SAP License:SAP MM中的几个概念
  12. 《NLTK基础教程——用NLTK和Python库构建机器学习应用》——2.10 练习
  13. c++如何生成指定范围的随机数
  14. 操作系统:操作系统装进U盘的图解教程
  15. python数据挖掘与分析
  16. 用python画科赫雪花
  17. 冒泡排序 java代码实现
  18. Roaring64NavigableMap(Bitmap)简单使用
  19. Bandwagon安装禅道记录
  20. c#如何实现软件授权后才能使用?

热门文章

  1. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #20 使用fio进行I/O的基准测试...
  2. ES transport client批量导入
  3. 在Azure上创建MYSQL服务
  4. 别总埋汰写代码,停下总结一下吧
  5. Know about Oracle RAC Heartbeat
  6. 你给我人脉,我给你全世界
  7. CAN总线简明易懂教程(三)
  8. XDC约束技巧——CDC篇
  9. DDR读写简介及相关
  10. php网站如何静态化链接,建站教程之网站URL静态化处理