How do I host a WPF control in a Windows Forms application?

First add references to the WPF namespaces (PresentationCore, PresentationFramework, UIAutomationProvider, UIAutomationTypes, and WindowsBase). Next create an instance of the ’ElementHost’ control and the control you wish to embed in the Windows Forms application and then hook that control up to the ElementHost control. Then simply add the ElementHost control to your Forms control collection : [C#] ElementHost host = new ElementHost(); System.Windows.Controls.ListBox wpfListBox = new System.Windows.Controls.ListBox(); for (int i = 0; i < 10; i++) { wpfListBox.Items.Add(‘Item ‘ + i.ToString()); } host.Dock = DockStyle.Fill; host.Controls.Add(wpfListBox); this.panel1.Controls.Add(host); However, if you want to use XAML to describe the WPF control that you want to use in the Windows Forms application, you need to add an ’Avalon’ UserControl item to your project. This will create a UserControl1.xaml file and a UserControl1.xaml.cs file. You can then modify the UserControl1.xaml file to contain whatever XAML you wish to have to describe your control. Then you could simply create an instance of this control and add it to the ElementHost control as in the above example : [C#] ElementHost host = new ElementHost(); UserControl1 uc1 = new UserControl1(); host.Controls.Add(uc1); host.Dock = DockStyle.Fill; this.panel1.Controls.Add(host); In addition, you will have to modify the project file because the Windows Application will not know what to do with the XAML file. You will have to open the project file (.csproj, .vbproj, etc.) in an editor like Notepad and then scroll to the bottom. You will see the following line: [XAML] <Import Project=’$(MSBuildBinPath)\Microsoft.CSharp.targets’ /> You will need to copy this line and paste it just below the above line and then change ‘CSharp’ to ‘WinFX’ so that the two lines look like: <Import Project=’$(MSBuildBinPath)\Microsoft.CSharp.targets’ /> <Import Project=’$(MSBuildBinPath)\Microsoft.WinFx.targets’ /> Now save this file and reload the project using VS and run the application.

How can I HwndSource Treats ComponentDispatcher Events ?

If the HwndSource is a top-level window (no parent HWND), it will register with ComponentDispatcher. If ThreadPreprocessMessage is raised and if the message is intended for the HwndSource or child windows, HwndSource calls its IkeyboardInputSink, TranslateAccelerator, TranslateChar, OnMnemonic keyboard sink sequence. If the HwndSource is not a top-level window (has a parent HWND), there will be no handling. Only the top-level window is expected to do the handling and there is expected to be a top-level window with keyboard sink support as part of any interoperation scenario. If WndProc on an HwndSource is called without an appropriate keyboard sink method being called first, your application will receive the higher level keyboard events such as ‘KeyDown’. However, no keyboard sink methods will be called which circumvents desirable keyboard input model features such as access key support. This might happen because the message loop did not properly notify the relevant thread on the ComponentDispatcher or because the parent HWND did not invoke the proper keyboard sink responses. A message that goes to the keyboard sink might not be sent to the HWND if you add hooks for that message by using the ‘AddHook()’ method. The message might have been handled at the message pump level directly and not submitted to the DispatchMessage function.

Does WPF support MDI ?

No. WPF from version 1 does not include support for MDI. If there is a need for MDI in your application, you can use the windows forms interop or create a custom child window manager.

How do I host Windows Forms control in a WPF Window ?

Windows forms can be hosted using the WindowsFormsHost markup extension of the ‘windowsformsintegration.dll’. References for ‘windowsformsintegration.dll’ and ‘System.Windows.Forms.dll’ have to be added. The following code snippet is used to add a control to the Windows Forms. Note: windowsformsintegration.dll will be available in the following location, Program files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll. [XAML] <WindowsFormsHost> <wf:MaskedTextBox x:Name=’mtxamt’ Mask=’00000.00’/> </WindowsFormsHost>

How do I create a child window in WPF ?

In WPF, any of the windows can be designated as child windows. A child window is like any other window but it gets minimized or closed when the parent is minimized or closed respectively. This type of window is called modeless dialog. To make a window a child of another window, the ‘Owner’ property of the child window has to be set as the parent window but only after the parent window is shown. Child windows of a window can be enumerated using the ‘OwnedWindows’ read-only property.