MekasDotNet
Skip Navigation Links
Home
---------------
Code Converter
---------------
Downloads
---------------
Forums
---------------
ReferenceSites
-------------
Contact Us

Using WPF control in WinForms:

1.In this example we will show how to insert a WPF control into a Windows Form.
2.Go to File-->New Project-->Name it as WPFControlinWinforms. It will display a default windows form.we set its Backcolor to pink.
3.Now we create a new WPF control and add it to our winform.
4.Go to solution Explorer-->Add Existing item, to add an existing WPF Control. Otherwise, design a new WPF Control by selecting-->Add New Item-->User Control(WPF)-->name it as UserControl1wpf as shown in figure below.

5.The design view,and the XAML code windows are shown below.
6.This wpf control displays a text message on a background picture.Line 7 defines the background image for the user control. In line 10, we use a text box control to insert text on the image, by making its Background transparent, the text box is invisible, only the text displayed in it can be seen.

7.To add this wpf control to the Windows form, go to the design mode,first drag and drop a panel control from the tool box on to the form. Then go to -->WPF Interoperability controls in the Toolbox and drag and drop a ElementHost control on to the panel.
8.By double-clicking the form, insert the following code in the page_load event of the form.

9.In the code above, line 12 declares the namespace for the wpf user control. In line24, a new instance of the usercontrol1wpf named mycontrol is declared. Lines 27 and 28 set the width and height of the elementhost control. Line 31 adds mycontrol as the child of the element host, and line 33 adds element host to the panel.
10.Finally, when we run the program, the user defined wpf control is displayed on the windows form as shown in figure below:

   
Your Feedback:
test1test118/23/2009 4:17:05 PM
Post Name:
Post E-mail:  
Post Comment: