1、添加两个引用:WindowsFormsIntegration.dll(负责整合WPF和Windows)、System.Windows.Forms.
2、在 XAML文件中添加两个引用(粗体部分):
<Window x:Class="CrossBowDemo.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integrationassembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Formsassembly=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>
<wf:DataGridView x:Name="Dg" Dock="Fill" SelectionMode="FullRowSelect">
</wf:DataGridView>
</wfi:WindowsFormsHost>
当时把WPF的控件canvas的handle传给C++的项目去刷新图像的时候发现最后刷新的是整个WPF页面而不是控件所在的区域,还不知道有什么办法可以解决。不过可以在WPF中嵌入WINFORM的图像控件PictureBox去做刷新。
嵌入方法:
1.在项目的References中加入WindowsFormsIntegration.dll和System.Windows.Form.dll
2.在页面的WPF中加入
[csharp] view plain copy
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integrationassembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Formsassembly=System.Windows.Forms"
比如
[csharp] view plain copy
x:Class="WpfApplication1.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integrationassembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Formsassembly=System.Windows.Forms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
title="MainWindow" Height="800" Width="1280" MouseMove="Window_MouseMove" MouseLeftButtonUp="Window_MouseLeftButtonUp" MouseDown="Window_MouseDown" MouseLeftButtonDown="Window_MouseLeftButtonDown">
然后我在Gridzhong 放下图像控件
[csharp] view plain copy
<Grid HorizontalAlignment="Left" Height="482" Margin="6,6,0,0" VerticalAlignment="Top" Width="737">
<wfi:WindowsFormsHost>
<wf:PictureBox x:Name="Cv_Main" Margin="0,0,0,0"></wf:PictureBox>
</wfi:WindowsFormsHost>
</Grid>
大功告成
WPF的MainWindow的代码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
namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent()
UserControl1 us = new UserControl1()
this.grid.Children.Add(us.addTextBox()) // 在前台的Grid里 添加Name属性,才可以使用 this.grid.........例如<Grid Name="grid">
}
}
}
winform的userControl的代码
using System.Collections.Generic
using System.ComponentModel
using System.Drawing
using System.Data
using System.Linq
using System.Text
using System.Windows.Forms
using System.Windows.Controls
namespace WpfApplication2
{
public partial class UserControl1 : System.Windows.Controls.Control
{
public UserControl1()
{
InitializeComponent()
}
private void UserControl1_Load(object sender, EventArgs e)
{
addTextBox()
}
public System.Windows.Controls.TextBox addTextBox()
{
System.Windows.Controls.TextBox tx = new System.Windows.Controls.TextBox()
tx.Text = "111"
return tx
}
}
}
UserControl1.Designer.cs 这个不改的话,你执行下,报错的地方删掉.
namespace WpfApplication2
{
partial class UserControl1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
}
因为WPF和WINFORM的控件类型是不一样的,一个是controls里的,一个是forms里的,你在WPF里添加 的话,类型不同,参数不能转换
其实我这样用,已经用的不是WINFORM的控件了,相当于自己建个类,写个创建控件的方法而已
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)