Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

WPF Interoperability


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.

[Click image to view at full size]
Figure 8: Adding ElementHost to the Toolbox in a Windows Forms project.

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;
  } 
 } 
} 
Listing Eight

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.

[Click image to view at full size]
Figure 9: A Windows Forms application containing a WPF Expander control.

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;
  } 
 } 
} 
Listing Nine

[Click image to view at full size]
Figure 10: The Windows Forms MonthCalendar is inside the WPF Expander, which is on a Windows Forms Form.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.