Use ElementHost to Add WPF to your Wndows Forms or put your WinForm controls in your WPF app using WindowsFormsHost

Many of you already have a Window Forms application. You can add WPF to your existing application quite easily using the ElementHost class, which can be added as a control to your form.


(You can also go the other way: host a Windows Forms control in a WPF element with the WindowsFormsHost , which can be added to your XAML)


This will bring you the ability to animate or scale parts of the UI for your applications. Notice how the Winform button rescales when you MouseWheel on the form!


The sample below is a Windows Form, which hosts a WPF Canvas, which has a TextBlock, Ellipse, and a WindowFormHost which contains a Windows Form button.


(You can also run this code in Foxpro, Excel, VB6! Do you see how?)


Start Visual Studio 2008

Choose File->New Project->Visual Basic->Windows Forms Application

Add references to







Paste in the code, then hit F5 to run.



The textblock opacity and Ellipse color are animated too. Try MouseWheel to see the objects grow/shrink.



See also

Animation Overview

WPF Animation: Use dynamic XAML to animate movies using XAMLReader or animate in code using BeginStoryBoard





<Sample Code>

Imports System.Windows.Forms.Integration

Imports System.Windows.Controls

Imports System.Windows.Media

Imports System.Windows.Input


Public Class Form1

    Sub New()


        ' This call is required by the Windows Form Designer.



        ' Add any initialization after the InitializeComponent() call.


        Me.Height = 600

        Me.Width = 800

        Dim elemHost As New System.Windows.Forms.Integration.ElementHost


        elemHost.Top = 30   ' move down a little bit


        Dim xaml = _

        <Canvas Name="MyPanel"





            <TextBlock Name="MyTextBlock" Canvas.Top="10">WPF Embedded Hi there


                    <EventTrigger RoutedEvent="TextBlock.Loaded">






                                    From="1.0" To=".10" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever"/>






            <Ellipse Height="150" HorizontalAlignment="Left" Canvas.Top="30">


                    <SolidColorBrush x:Name="ebrush" Color="Black"/>



                    <EventTrigger RoutedEvent="Ellipse.Loaded">




                                <DoubleAnimation From="20" To="200" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever"/>




                                    From="Red" To="Blue" Duration="0:0:3" AutoReverse="True" RepeatBehavior="Forever"/>







        elemHost.Child = CType(System.Windows.Markup.XamlReader.Load(xaml.CreateReader), System.Windows.UIElement)


        Dim MyCanvas = CType(elemHost.Child, Canvas)

        elemHost.Height = 400

        elemHost.Width = 600

        Dim btn As New Windows.Forms.Button ' This is a WinForms button: not a wpf System.Windows.Controls.Button

        btn.Visible = True

        btn.Text = "Winform Btn to launch WPF form"

        btn.AutoSize = True

        btn.BackColor = System.Drawing.Color.Bisque

        Dim WinFormHost = New WindowsFormsHost

        WinFormHost.Child = btn

        AddHandler btn.Click, AddressOf btn_Click

        Canvas.SetTop(WinFormHost, 80)



        MyCanvas.RenderTransform = New ScaleTransform(1, 1)

        AddHandler MyCanvas.MouseWheel, AddressOf MyCanvas_MouseWheel



    End Sub

    Sub btn_Click()

        Dim oWPFForm = New WPFForm


    End Sub

    Sub MyCanvas_MouseWheel(ByVal o As Canvas, ByVal e As MouseWheelEventArgs)

        Dim tr = CType(o.RenderTransform, ScaleTransform)

        If e.Delta > 0 Then

            tr.ScaleX *= 1.1

            tr.ScaleY *= 1.1


            tr.ScaleX /= 1.1

            tr.ScaleY /= 1.1


        End If


    End Sub

End Class


Class WPFForm : Inherits Windows.Window

    Sub New()

        Dim xaml = _






            <TextBlock>WPFForm Hi there</TextBlock>


        Me.Content = System.Windows.Markup.XamlReader.Load(xaml.CreateReader)

    End Sub



End Class

</Sample Code>


Comments (7)

  1. My prior post showed how to create XAML WPF and put it on your Winform App. We can go one step further:

  2. My prior post showed how to create XAML WPF and put it on your Winform App. We can go one step further

  3. Anonymous says:

    When I host my usercontrol in WPF, everything is working fine except tooltip (auto). In winform when I keep Ellipsis and AutoSize property True and False resp. its working fine in winform but not in hosted wpf. What might be the reason?

Skip to main content