[转]在WPF中使用WinForm控件方法
本文转自:http://blog.csdn.net/lianchangshuai/article/details/6415241
下面以在Wpf中添加ZedGraph(用于创建任意数据的二维线型、条型、饼型图表的一个开源类库)控件,说明在WPF中使用Winform控件的方法。
1、 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll。
2、 由于要用到ZedGraph控件,所以也要添加对ZedGraph.dll的引用。
3、 在要使用WinForm控件的WPF窗体的XAML文件中添加如下内容(选中部分):
即: xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:zedgraph="clr-namespace:ZedGraph;assembly=ZedGraph"
4、 在WPF的容器控件内如Grid内首先要添加WinForm控件的宿主容器,用于衔接WPF和WinForm,
对应XAML如下:
<Grid>
<wfi:WindowsFormsHost Width="300" HorizontalAlignment="Right">
<wf:Label x:Name="lblName" Text="winForm控件在此” />
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<wf:Button x:Name="nnn" Text="确定”/>
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<zedgraph:ZedGraphControl x:Name="zedGraphControl" />
</wfi:WindowsFormsHost>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
说明:<wfi:WindowsFormsHost></wfi:WindowsFormsHost>即为WinForm控件的宿主容器,每一个宿主容器只能放一个WinForm控件,如下例,放了三个WinForm控件,分别放在三个宿主容器里面,该容器可以设置属性来调整大小和布局。
注意:如上我添加的WinForm控件如在指定其Name时,必须加前缀x:,如添加Lable时<wf:Label x:Name="lblName" Text="我是WPF中的WinForm控件” />,否则后台代码无法访问。
5、 如果要在WPF后台代码中访问上面的Lable,可直接像在WinForm中使用一样。如在点击某一按钮时改变Lable内容,代码如下:lblName.Text=”内容已改变”;
6、 以使用WinForm控件ZedGraph绘制曲线为例,说明后台用法:
在后台用Using添加对System.Windows.Forms和ZedGraph和System.Drawing命名空间的引用,完整后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using ZedGraph;
using System.Drawing;
namespace ZedGraphAtWpf2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
lblName.Text =”我是WinForm控件”;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ZedGraph.GraphPane myPane = zedGraphControl.GraphPane;
myPane.Title.Text = "My Test Graph/n(For CodeProject Sample)";
myPane.XAxis.Title.Text = "My X Axis";
myPane.YAxis.Title.Text = "My Y Axis";
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
for (int i = 0; i < 36; i++)
{
double x = (double)i + 5;
double y1 = 1.5 + Math.Sin((double)i * 0.2);
double y2 = 3.0 * (1.5 + Math.Sin((double)i * 0.2));
list1.Add(x, y1);
list2.Add(x, y2);
}
// Generate a red curve with diamond
// symbols, and "Porsche" in the legend
LineItem myCurve = myPane.AddCurve("Porsche",
list1, System.Drawing.Color.Red, SymbolType.Diamond);
// Generate a blue curve with circle
// symbols, and "Piper" in the legend
LineItem myCurve2 = myPane.AddCurve("Piper",
list2, System.Drawing.Color.Blue, SymbolType.Circle);
zedGraphControl.AxisChange();
}
}
}
完整XAML如下:
<Window x:Class="ZedGraphAtWpf2.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:zedgraph="clr-namespace:ZedGraph;assembly=ZedGraph"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<wfi:WindowsFormsHost Width="300" HorizontalAlignment="Right">
<wf:Label x:Name="lblName" Text="WinForm控件在此” />
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<wf:Button x:Name="nnn" Text="确定” />
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<zedgraph:ZedGraphControl x:Name="zedGraphControl" />
</wfi:WindowsFormsHost>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
[转]在WPF中使用WinForm控件方法的更多相关文章
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- 在WPF中调用Winform控件
最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...
- wpf中插入winform控件并获取句柄
因工作需要使用wpf做界面,而有个开发包依赖picturebox控件,上网研究了一下,总算弄通了. 首先在项目中添加引用System.Windows.Forms与WindowsFormsIntegra ...
- 如何在WPF中调用Winform控件
原文地址:http://hi.baidu.com/stuoopluwqbbeod/item/32ec38403da42ee2bcf45167 功能实现主要分三步:1.添加两个引用:WindowsFor ...
- Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决
有人会说不建议Wpf中使用Winform控件,有人会说建议使用Winform控件在Wpf下的替代方案,然而在实际工作中由于项目的特殊需求,考虑到时间.成本等因素,往往难免会碰到在WPF中使用Winfr ...
- WPF保存包含Winform控件的XAML页面问题
原文:WPF保存包含Winform控件的XAML页面问题 最近的工作中,用到了WPF调用Winform控件 但是在保存XAML页面的时候发现了问题,就是Winform页面黑黑的,没有任何渲染的波形曲线 ...
- WPF中的image控件的Source赋值
WPF中的Image控件Source的设置 1.XAML中 简单的方式(Source="haha.png"); image控件的Source设置为相对路径后(Source=&quo ...
- WPF中的ControlTemplate(控件模板)(转)
原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html WPF中的ControlTemplate(控件模板) ...
- WPF中的ControlTemplate(控件模板)
原文:WPF中的ControlTemplate(控件模板) WPF中的ControlTemplate(控件模板) ...
随机推荐
- JdkDynamicAopProxy源码
JdkDynamicAopProxy是通过接口实现动态代理类,主要方法是getProxy(ClassLoader classLoader), 代理类生成之后再调用目标方法时就会调用invoke方法. ...
- CDocument类的UpdateAllViews()成员函数
(一)UpdateAllViews() 与 Invalidate()的区别 UpdateAllViews()是在DOC/VIEW结构中,当一个视图的数据改变后,通知所有视图作相应的改变,和重画毫无关系 ...
- dojo grid 分页
dojo很强大,也很方便,但是缺少文档,只能看源代码,也挺好的,就是费时间... 网上找了一段代码(找不到原出处了,不好意思),也看了dojo自带的demo,放一段可以执行的页面代码这里.把ip换成自 ...
- 全代码实现ios-2
全代码开发ios第一个应用程序,完全像是盲人摸象. 首先,要设计这个应用,无论从界面,还是颜色搭配,以及功能,都是一个人完成. 也许,做独立开发人真的相当不容易. 因为,没有人帮忙给意见,而且,也没有 ...
- IIS网站程序无法访问oracle
系统环境: win7 + iis7 asp.net应用程序访问oracle数据库时,报ORA-12560: TNS: 协议适配器错误. 使用c/s程序访问数据库没有问题,plsql也没问题,说明ora ...
- lvs-dr模式原理详解和可能存在的“假负载均衡”
原文地址: http://blog.csdn.net/lengzijian/article/details/8089661 lvs-dr模式原理 转载注明出处:http://blog.csdn.net ...
- 用ICSharpCode.SharpZipLib进行压缩
今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...
- js中的this和apply
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 1.纯粹函数调用. function test() { this. ...
- 一步步学Mybatis-实现简单的分页效果逻辑 (5)
在前四章中我们已经基本完成了对单表的CRUD与多表联合查询方式的Mybatis操作与配置方式,今天这里要讲的是关于一个业务问题中我们常碰到的分页问题.在开发web项目的时候我们经常会使用到列表显示,一 ...
- System.Data.SqlClient.SqlError: 备份集中的数据库备份与现有的 'XXX' 数据库不同
System.Data.SqlClient.SqlError: 备份集中的数据库备份与现有的 'XXX' 数据库不同. 1. 删除与要恢复数据库同名的已经存在的数据库:2. 右击“数据库”选择“还原数 ...