如何在WPF中调用Winform控件
2013-03-27 12:11
330 查看
转自:/article/6020051.html
http://hi.baidu.com/aqiang000000/blog/item/05507e3692c9b4daa3cc2ba3.html
功能实现主要分三步:
1、添加两个引用:WindowsFormsIntegration.dll (负责整合WPF和Windows)、System.Windows.Forms.
2、在 XAML文件中添加两个引用(粗体部分):
<Window x:Class="CrossBowDemo.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hosting Windows Forms Control In WPF"
Height="300"
Width="650"
ResizeMode="NoResize"
Loaded="WindowLoadedHandler"
>
</Window>
3、在XAML编码区实现你想添加的控件:
原文添加的是 DataGridView控件:
<wfi:WindowsFormsHost>
<!-- Set some properties on Windows Forms control in Xaml-->
<wf:DataGridViewx:Name="dataGridView"
Dock="Fill" SelectionMode="FullRowSelect"/>
</wfi:WindowsFormsHost>
效果图:
本人添加的是 NumericUpDown控件:
<GridHeight="0"
Margin="146,0,0,116" MinHeight="20" MinWidth="20"
Name="grid1" VerticalAlignment="Bottom" HorizontalAlignment="Left"
Width="50">
<wfi:WindowsFormsHost>
<wf:NumericUpDownx:Name="nupCounter"
Maximum="100"></wf:NumericUpDown>
</wfi:WindowsFormsHost>
</Grid>
效果图:
在本人的代码中Grid的作用相当于Web页面中用来布局的Table。 此处加上Grid是为了方便移动控件的位置。
另一种方法是在后台添加,下面转载自:http://www.2cto.com/kf/201212/173291.html
wpf 中加载winform控件(不是窗体,必须是继承自Control类)
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using Fibonacci; //这个好像不用加
namespace WPFHost
{
/// <summary>
/// Interaction logic for Page1.xaml
/// </summary>
public partial class Page1 : Page
{
private readonly MainForm mainForm = new MainForm();
public Page1()
{
InitializeComponent();
//Create a Windows Forms Host to host a form
WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
stackPanel.Width = mainForm.Width; //stackPanel是前台xaml的一个控件
stackPanel.Height = mainForm.Height;
windowsFormsHost.Width = mainForm.Width;
windowsFormsHost.Height = mainForm.Height;
mainForm.TopLevel = false;
WindowsFormsHost.Child = mainForm;
stackPanel.Children.Add(windowsFormsHost);
}
}
}
最后,关于加过去的winform主题回到旧的主题的样式的问题:
这个问题解决了,重装启动事件就行了。
protected override void OnStartup(StartupEventArgs e)
{
// Raises the Startup event.
base.OnStartup(e);
System.Windows.Forms.Application.EnableVisualStyles();
}
http://hi.baidu.com/aqiang000000/blog/item/05507e3692c9b4daa3cc2ba3.html
功能实现主要分三步:
1、添加两个引用:WindowsFormsIntegration.dll (负责整合WPF和Windows)、System.Windows.Forms.
2、在 XAML文件中添加两个引用(粗体部分):
<Window x:Class="CrossBowDemo.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hosting Windows Forms Control In WPF"
Height="300"
Width="650"
ResizeMode="NoResize"
Loaded="WindowLoadedHandler"
>
</Window>
3、在XAML编码区实现你想添加的控件:
原文添加的是 DataGridView控件:
<wfi:WindowsFormsHost>
<!-- Set some properties on Windows Forms control in Xaml-->
<wf:DataGridViewx:Name="dataGridView"
Dock="Fill" SelectionMode="FullRowSelect"/>
</wfi:WindowsFormsHost>
效果图:
本人添加的是 NumericUpDown控件:
<GridHeight="0"
Margin="146,0,0,116" MinHeight="20" MinWidth="20"
Name="grid1" VerticalAlignment="Bottom" HorizontalAlignment="Left"
Width="50">
<wfi:WindowsFormsHost>
<wf:NumericUpDownx:Name="nupCounter"
Maximum="100"></wf:NumericUpDown>
</wfi:WindowsFormsHost>
</Grid>
效果图:
在本人的代码中Grid的作用相当于Web页面中用来布局的Table。 此处加上Grid是为了方便移动控件的位置。
另一种方法是在后台添加,下面转载自:http://www.2cto.com/kf/201212/173291.html
wpf 中加载winform控件(不是窗体,必须是继承自Control类)
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using Fibonacci; //这个好像不用加
namespace WPFHost
{
/// <summary>
/// Interaction logic for Page1.xaml
/// </summary>
public partial class Page1 : Page
{
private readonly MainForm mainForm = new MainForm();
public Page1()
{
InitializeComponent();
//Create a Windows Forms Host to host a form
WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
stackPanel.Width = mainForm.Width; //stackPanel是前台xaml的一个控件
stackPanel.Height = mainForm.Height;
windowsFormsHost.Width = mainForm.Width;
windowsFormsHost.Height = mainForm.Height;
mainForm.TopLevel = false;
WindowsFormsHost.Child = mainForm;
stackPanel.Children.Add(windowsFormsHost);
}
}
}
最后,关于加过去的winform主题回到旧的主题的样式的问题:
这个问题解决了,重装启动事件就行了。
protected override void OnStartup(StartupEventArgs e)
{
// Raises the Startup event.
base.OnStartup(e);
System.Windows.Forms.Application.EnableVisualStyles();
}
相关文章推荐
- 如何在WPF中调用Winform控件 (转)
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- wpf 如何在一个页面中调用另一个页面的函数
- WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- [WPF]MVVM模式下如何在后台cs中调用绑定命令
- 【转】WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变
- 如何在WPF中嵌入Winform控件
- WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变
- C#下WPF中调用WinForm控件
- WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变
- 如何在WPF中调用Winform控件
- WPF 调用WinForm控件
- 如何在WPF中调用C#控件库(HexEdit)