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:
- Add references to System.Windows.Forms and WindowsFormsIntegration dlls.
- Add a reference to the dll containing the Windows Form control you are going to be placing in your WPF app.
- Add a new xml namespace (xmlns) attribute to the root element of your xaml document … (usually a Window element)
<Windowxmlns=”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.
- Add a WindowsFromsHost element in the panel where you want to insert your Winforms control.
- 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.
- Drag an instance of the ElementHost control on to your Winforms application wherever you wish to insert WPF content.
- 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.
March 25, 2008 at 5:25 am
thanks much, brother
September 16, 2008 at 11:29 am
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
September 16, 2008 at 1:32 pm
Every Framework Element in WPF and Silverlight has a Name property. Did you capitalize the ‘N’? … control.Name not control.name
November 10, 2008 at 12:19 pm
thanks, thats what i’ve been looking for