惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Security Latest
Security Latest
S
Secure Thoughts
H
Heimdal Security Blog
The Last Watchdog
The Last Watchdog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
PCI Perspectives
PCI Perspectives
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
爱范儿
爱范儿
腾讯CDC
P
Privacy & Cybersecurity Law Blog
量子位
T
Threat Research - Cisco Blogs
V
V2EX
S
Schneier on Security
P
Proofpoint News Feed
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
月光博客
月光博客
C
Cybersecurity and Infrastructure Security Agency CISA
Schneier on Security
Schneier on Security
L
LINUX DO - 最新话题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
S
SegmentFault 最新的问题
G
GRAHAM CLULEY
人人都是产品经理
人人都是产品经理
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tenable Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - umlchina

.Net桌面程序自动更新NAppUpdate c# interview examination questions prism框架里module太多启动速度过慢 Localizing of WPF application Unity 中的拦截功能 Interception and Interceptors in C# (Aspect oriented programming) 转载 WPF Snippet Tutorial - Aligning ListView Items WPF Tutorial - How To Use A DataTemplateSelector Dependency Properties Introduction To Dependency Properties of WPF 朋友的公司招.net程序员2名及项目经理1名 Simple Object Access Protocol (SOAP) 1.1 how to create pdf by itextsharp convert RGB colorspace to Homochromy(Black/whit) - umlchina 图片处理转换 how to design a hardware service use .net remoting : 使用SAAJ发送和接收SOAP消息 consume an asp.net webservice(upload a file to server) from java via soap 紧急求助:我们想做一个service程序,这个service负责统一管理这台机器上装的一些设备(包括签字板,扫描仪等等),
how to use wpf control in a winform application
umlchina · 2012-04-24 · via 博客园 - umlchina

A little while back, we did a tutorial on how to embed WinFoms controls inside of WPF application (you can read all about it here). Today, we are going to flip that on its head - we are going to take a look at how to embed WPF controls inside of a WinForms application.

You might be thinking to yourself "why would someone want to do that?" A great question, and there are a couple different reasons. First, perhaps you have an application already written in WinForms, but you want to move to WPF. Instead of rewriting the entire thing from scratch (which might be a large amount of work, and might be time/cost prohibitive), you can rewrite portions of the application as time allows or when you need to redo a particular section anyway.

Another reason would be if there is a area of your app that would be easy to do in WPF (maybe because of the animation features, or the composibility) while in WinForms it would be difficult.

So what do we need to do to enable this integration? Well, first off, we need to add a reference to the Visual Studio project. You do this by right clicking on "References" in the Solution Explorer and choosing "Add Reference":

Add Reference Context Menu

Once you have the dialog up, you want to scroll to the bottom on the .NET tab and choose the "WindowsFormsIntegration" component, and hit OK:

Add Reference Dialog

At this point, you probably want to create the WPF user control that you want to add to your app. If its already written, you can add it through the Add Existing Item menu option, and if you need to write one, you can choose "Add New Item" and under WPF choose "User Control". In my case I want to create a new one, and I'm going to name it "MyWPFControl":

Add New Item Dialog

So my made-up reason why I want a WPF control in my WinForms app is that I want a custom combo box - or really a combo box with some custom items. While this is doable in WinForms, it is hard. Downright annoying in many ways. In WPF, though, its really easy, since you can put whatever you want in aComboBoxItem. In this case, my user control consists of a combo box with three items - a circle, a triangle, and a rectangle:

<UserControl x:Class="WPFInWinForms.MyWPFControl"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Height="300" Width="300">
  <Grid>
    <ComboBox>
      <ComboBoxItem Margin="2">
        <Ellipse Width="20" Height="20" Stroke="Black" />
      </ComboBoxItem>
      <ComboBoxItem Margin="2">
        <Path Stroke="Black" Data="M 0,0 L 20,0 L 10,20 Z" />
      </ComboBoxItem>
      <ComboBoxItem Margin="2">
        <Rectangle Stroke="Black" Width="20" Height="20" />
      </ComboBoxItem>
    </ComboBox>
  </Grid>
</UserControl>

Ok, now that we have the control we want to add, how do we add it? Well, there are two ways of going about this - one is through the designer, and the other is by adding the control manually in code. We are fist going to take a look at the designer way of doing things.

When we added that reference earlier, one of the things it did was give us access to a control calledElementHost. This control is for WPF in WinForms what the WindowsFormsHost was for WinForms in WPF. In the designer, you can find this control in the Toolbox underneath "WPF Interoperability":

WPF Interoperability Toolbox Section

So once you drag an ElementHost onto your form, the designer makes things really easy for you. You just click on the little arrow on the upper right of the ElementHost and you get a couple options. The one we care about is the combobox for "Select Hosted Content". When you click this, you get to choose what WPF control you want to host in the ElementHost:

ElementHost options

Once you do that, well, thats it. You can run the app, and the WPF control will show up exactly where you put it:

WPF in WinForms

Doing all of this in code is also quite easy. In fact, I'll just throw all the code up here in one block:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

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

      ElementHost elhost = new ElementHost();
      elhost.Size = new Size(11060);
      elhost.Location = new Point(45,35);

      MyWPFControl wpfctl = new MyWPFControl();
      elhost.Child = wpfctl;

      this.Controls.Add(elhost);
    }
  }
}

So the first thing needed is that last using statement. The ElementHost lives in that namespace, so we add a using statement for it. After that, we just create a new ElementHost, and set its normal WinForms properties. Then we create an instance of the wpf control we want, and add that as the child for theElementHost. Finally, we add the ElementHost as a child to the current form - and we are all done!

So that is it for using WPF controls inside of Windows Forms. You can download the little sample app fromhere if you would like to play with the code yourself. As always, if you have any questions or comments, feel free to leave them below.