Crossbow Design Time : Using WPF Controls on WinForms Designer

 Crossbow aims to provide interoperability between windows forms and WPF.  In my previous blog https://blogs.msdn.com/nagasatish/archive/2007/07/16/crossbow-interoperability-between-windows-forms-and-wpf.aspx, i talked about a windows forms control ElementHost that is used to host the WPF control in it and display it on a windows forms app.
 Our team worked on providing a design time story for this.  The aim is to host WPF controls on a windows Forms App just like any other windows forms controls like button, datagridview etc.  In this blog, i will describe the steps involved in doing so.

They include
1) Create a Winforms Application
2) Add a User Control (WPF) item to the project.
3) Design the WPF user Control using the WPF designer. 
4) Build the App
Now there are two ways to add the WPF user control to the project. 

The first way is
a) Drop the Element Host Control form the WPF interoperability Tab on to the form
b) From the Smart tag, set the the hostedcontent to UserControl (WPF)

The other way is
a) Drop the UserControl from the toolbox on the form. When you do this, an Element Host is automatically created and its hostedcontent is set to WPF User Control

Next, I would like to hook events to the WPF User Control
We make use the HostedContentName property in achieving this.  Suppose, we have a WPF Button hosted in the element host and we want to show a message box on mouse enter event. We can do this as follows.
In the constructor of the form, we add the mouse enter event for the HostedContentName object. 
        public Form1()
        {
            InitializeComponent();
            userControl11.MouseEnter += new System.Windows.Input.MouseEventHandler(userControl11_MouseEnter);
        }

        void userControl11_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            MessageBox.Show("Mouse Entered");
        }

This is how we create wpf controls and attach events to them and host them in windows forms applications

Comments

  • Anonymous
    June 13, 2008
    Well I'am working on a project where I have to switch between 2 forms using user control..i have created a design on user control but I want that if I click the button...the user control triggers and its page appears on the same form.........tell me how can I call that form? reply on bilal_saeed2000@hotmail.com