In the following sample we’ll see how to host a WinForms PropertyGrid control in WPF window.
Also, If you need a property-grid that can’t change (read-only), here are the basics of how to do it.
REMARK: It’s not fully done yet (you can still right-click a value and select ‘cut’ or ‘delete’ which alter it), if you have any idea how to prevent this menu, please, let us all know by posting a reply.
Hosting the WinForm PropertyGrid in WPF
The XAML
IMPORTANT Notes
Add reference to:
- System.Windows.Forms
- WindowsFormsIntegration
Note |
---|
WindowsFormsIntegration.dll is installed with the Windows Presentation Foundation (WPF) assemblies. The default location for the assembly is %programfiles%Reference AssembliesMicrosoftFrameworkv3.0WindowsFormsIntegration.dll. |
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:swf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Loaded="Window_Loaded" Title="Hosting a Windows Forms Property Grid in WPF" Height="350" Width="525"> <Grid> <WindowsFormsHost> <swf:PropertyGrid x:Name="propertyGrid" /> </WindowsFormsHost> </Grid> </Window>
The code
using System.Windows; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { propertyGrid.SelectedObject = this; } } }
The Result
Read Also
Layout Considerations for the WindowsFormsHost Element
Resources
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx
http://blog.typps.com/2011/04/getting-most-out-of-winforms.html