Embedding a PropertyGrid with XAML
There's no reason that you have to instantiate a WindowsFormsHost instance in procedural code; you could alternatively define it right inside your XAML file. Furthermore, there's nothing to stop you from using Windows Forms controls inside of XAML, other than limitations of the expressiveness of XAML. (The controls must have a default constructor, useful instance properties to set, and so on.)
Not all Windows Forms controls work well within XAML, but PropertyGrid works reasonably well. For example, the previous XAML can be replaced with the following XAML:
<Window x:Class=.Window1" xmlns=://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=://schemas.microsoft.com/winfx/2006/xaml" xmlns:swf="clr- namespace:System.Windows.Forms;assembly=System.Windows.Forms" Title=a Windows Forms Property Grid in WPF" Loaded=_Loaded"> <Grid> <WindowsFormsHost> <swf:PropertyGrid x:Name="propertyGrid"/> </WindowsFormsHost> </Grid> </Window>
The System.Windows.Forms.Integration .NET namespace is already included as part of WPF's standard XML namespace, so WindowsFormsHost can be used without any additional work. And with the System.Windows.Forms .NET namespace given the prefix of swf, the PropertyGrid object can be instantiated directly in the XAML file. The PropertyGrid can be added as a child element to WindowsFormsHost because its Child property is marked as a content property. PropertyGrid's properties can generally be set in XAML rather than C#. We still need some procedural code, however, to set the SelectedObject property:
private void Window_Loaded(object sender, RoutedEventArgs e) { propertyGrid.SelectedObject = this; }
Embedding WPF Controls in Windows Forms Applications
WPF controls can be embedded inside a Windows Forms application thanks to a companion class of WindowsFormsHost called ElementHost. ElementHost is like HwndSource, but is customized for hosting WPF elements inside a Windows Forms Form rather than inside an arbitrary HWND. ElementHost is a Windows Forms control (deriving from System.Windows.Forms.Control) and internally knows how to display WPF content.
To demonstrate the use of ElementHost, I create a simple Windows Forms application that hosts a WPF Expander control. After creating a standard Windows Forms project in Visual Studio, the first step is to add ElementHost to the Toolbox using the Tools, Choose Toolbox Items menu item. This presents the dialog in Figure 8.

With ElementHost in the Toolbox, you can drag it onto a Windows Form just like any other Windows Forms control. Doing this automatically adds references to the necessary WPF assemblies (PresentationFramework.dll, PresentationCore.dll, and so on). Listing Eight is the main source file to a Windows Forms project, whose Form contains an ElementHost called elementHost docked to the left and a Label on the right.
using System.Windows.Forms; using System.Windows.Controls; namespace WindowsFormsHostingWPF { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Create a WPF Expander Expander expander = new Expander(); expander.Header = "WPF Expander"; expander.Content = "Content"; // Add it to the ElementHost elementHost.Child = expander; } } }
This code uses the System.Windows.Controls namespace for Expander, which it simply instantiates and initializes inside the Form's constructor. ElementHost, like WindowsFormsHost, has a simple Child property that can be set to any UIElement. This property must be set in source code rather than in the Windows Forms designer, so here it is set to the Expander instance. The result is shown in Figure 9. Notice that, by default, the Expander occupies all the space given to the ElementHost.

Taking this example a step further, you can use a combination of ElementHost and WindowsFormsHost to have a Windows Forms control embedded in a WPF control embedded in a Windows Forms application! All we need to do is set the Content of the WPF Expander to a WindowsFormsHost, which can contain an arbitrary Windows Forms control. Listing Nine does just that, placing a Windows Forms MonthCalendar inside a WPF Expander, all on the same Windows Forms Form. Figure 10 shows the result.
using System.Windows.Forms; using System.Windows.Controls; using System.Windows.Forms.Integration; namespace WindowsFormsHostingWPF { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Create a WPF Expander Expander expander = new Expander(); expander.Header = "WPF Expander"; // Create a MonthCalendar and wrap it in a WindowsFormsHost WindowsFormsHost host = new WindowsFormsHost(); host.Child = new MonthCalendar(); // Place the WindowsFormsHost in the Expander expander.Content = host; // Add the Expander to the ElementHost elementHost.Child = expander; } } }
