Question:

“I have been working with Windows Forms for a long time. I have a lot of good Winforms user controls that I want to keep using, but I would like to start using WPF to create better user interfaces. Is this an all or nothing deal? Can I create Winforms apps which use some WPF controls or WPF apps which use some of my existing Winforms controls?”

 

 

Answer:

No. This is not an all or nothing deal. You can leverage WPF in your existing Winform applications and vice versa.

Embedding a Winforms control in a WPF application:

  1. Add references to System.Windows.Forms and WindowsFormsIntegration dlls.
  2. Add a reference to the dll containing the Windows Form control you are going to be placing in your WPF app.
  3. Add a new xml namespace (xmlns) attribute to the root element of your xaml document … (usually a Window element)


    <Window

    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

    . . .

     xmlns:zed=”clr-namespace:ZedGraph;assembly=ZedGraph”  

    In this example I am inserting a ZedGraph control. It is third party control used to graph out the movements of a Wiimote device. The class defined in the ZedGraph assembly which is in the ZedGraph namespace. I am using zed as a shorthand to reference that assembly.

  4. Add a WindowsFromsHost element in the panel where you want to insert your Winforms control.
  5. Add your Windforms control as the content of that WindowsFormsHost element.

     <WindowsFormsHost>

    <zed:ZedGraphControl x:Name=”graphControl”/>               

    </WindowsFormsHost>

    Warning:  If you have the attribute AllowsTransparency=True on the root Window element, the WindowsFormsHost will not be displayed. You will not get any compiler errors, but the content will simply not be displayed. The way WPF handles transparency is not supported by Windows Forms, so if the window allows transparency, the Winforms content cannot be properly rendered… I learned this the hard way. It took a while to figure out why my controls weren’t displaying on my GUI!

    Embedding a WPF control in a Winforms application:

 First, add ElementHost to the VisualStudio Toolbox going to the Tools menu and selecting Choose Toolbox Items.  A dialog is shown with a  list of controls that can be added to the toolbox. Make sure ElementHost is checked and click Ok.

Note: By dragging the ElementHost on to the form using the Winforms designer, Visual Studio automatically adds references to the necessary references to the necessary WPF assemblies like PresentationFramework.dll, PresentationCore.dll, etc.

 

  1. Drag an instance of the ElementHost control on to your Winforms application wherever you wish to insert WPF content.
  2. Create your WPF content (in the code behind) and set it as the Child of your ElementHost object.

    public

    Public Form1()

    {

    InitializeComponent();

    //Create WPF Content

    Viewer viewer = new Viewer();

    elementHost.Child = viewer;

    }

    Note: Viewer is a User control I created in WPF. In order for me to use it I added a reference to the assembly where the Viewer control was defined. In the above example, I also named the instance of the ElementHost object I placed on the GUI as elementHost.

Advertisement

4 Responses to “”

  1. Ryleelw Says:

    thanks much, brother

  2. ditto Says:

    do you know how I can get the control name? my ZedGraphControl’s name is “zgc”, and control.name == “zgc” didn’t work for me.. thx

  3. wpfguiblog Says:

    Every Framework Element in WPF and Silverlight has a Name property. Did you capitalize the ‘N’? … control.Name not control.name

  4. Airborn Says:

    thanks, thats what i’ve been looking for :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.