您的位置:首页 > 移动开发

windows窗体程序中使用WPF控件 Host WPF Controls in Windows Forms Application

2013-06-15 15:28 691 查看
参考http://keyvan.io/host-wpf-controls-in-windows-forms

在windows form appliation中添加wpf空间,需要使用一个ElementHost的容器,接着将创建出来的WPF对象赋值到ElementHost的child属性中,类似子控件添加到Panel或者Form的controls容器中,可以ElementHost只能对应一个wpf控件,接着将ElementHost添加到父级Controls中。

实现如下:

1. 创建Windows Form Application项目,命名为HostWPFWinForm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace HostWPFWinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

// Add WPF Control
MyWPFControl control = new MyWPFControl();
ElementHost host = new ElementHost();
host.Top = 0;
host.Left = 0;
host.Width = 300;
host.Height = 40;
host.Child = control;
this.Controls.Add(host);
}
}
}
[/code]6. 编译并且运行程序,效果图如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: