博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在WPF中使用WinForm控件方法
阅读量:7218 次
发布时间:2019-06-29

本文共 1573 字,大约阅读时间需要 5 分钟。

原文:

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如下:

说明:<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{    ///     /// Window4.xaml 的交互逻辑    ///     public partial class Window4 : Window    {        public Window4()        {            InitializeComponent();        }                private void wfButton_Click(object sender, EventArgs e)        {            wfLabel.Text= "内容已改变";        }    }}

完整XAML如下:

 

转载地址:http://zsxym.baihongyu.com/

你可能感兴趣的文章
【转载】jQuery的.live()和.die()
查看>>
函数式编程--函数式接口
查看>>
python--常用模块calendar
查看>>
register form
查看>>
Java中的clone
查看>>
Lucene基础(2)
查看>>
Oracle 存储过程
查看>>
java基础 静态 static 问在多态中,子类静态方法覆盖父类静态方法时,父类引用调用的是哪个方法?...
查看>>
FlasCC发布说明
查看>>
如何在macOS Sierra中运行CORE Keygen破解程序
查看>>
终极解决方案:windows10资源管理器假死
查看>>
【java】一维数组循环位移方阵
查看>>
Essential Studio for mobile MVC中创建Razor应用程序平台教程
查看>>
java主函数的含义
查看>>
中国大学MOOC —— 学习笔记(四)
查看>>
访问,ringbtn,
查看>>
致橡树
查看>>
一段测试代码,哦哦哦,
查看>>
uiimagepickercontroller,中文,--》摘
查看>>
第四次作业
查看>>