Is WindowsFormsHost fit for purpose (.net WPF hosting WinForms)? Is WindowsFormsHost fit for purpose (.net WPF hosting WinForms)? wpf wpf

Is WindowsFormsHost fit for purpose (.net WPF hosting WinForms)?


We're currently using WindowsFormsHost in our software to host the WinForms DataGridView control, and we've not had any real problems with it. A few things to watch out for though:

The first is the air-space restrictions. Practically speaking, this means that WinForms content always appears on top of WPF content. So if you are using WPF adorners they will appear to be "trimmed" if they hit up against a WinForms region in your app.

The second is that, because they use Windows resources, you have to manage the lifetimes of WinForms components more carefully. Unlike WPF components, WinForms controls expect to be Disposed when they're finished with. This makes it tricky to include them in a pure XAML view.

The last thing is that WinForms controls don't seem to resize as smoothly as the rest of the WPF display: they tend to snap to their new size once you've finished making an adjustment.


One problem I've run into is that embedded Win Forms controls do not participate in any transform operations applied to their WPF container. This results in visual flashing effects and the embedded control appearing in an innappropriate location. I worked around this by binding the visibility of the Windows Forms Host to the animation state of its WPF container, so that the embedded control was hidden until the animation completed, like below.

<WindowsFormsHost Grid.Row="1" Grid.Column="1" Margin="8,0,0,0"     Visibility="{Binding ActualHeight, RelativeSource={RelativeSource     Mode=FindAncestor, AncestorType=UserControl},     Converter={StaticResource WinFormsControlVisibilityConverter}}" >     <winforms:DateTimePicker x:Name="datepickerOrderExpected" Width="140"        Format="Custom" CustomFormat="M/dd/yy  h:mm tt"        ValueChanged="OnEditDateTimeOrderExpected" /></WindowsFormsHost>


I hosted WPF controls in WinForms and vice versa without problems. Though, I would test such scenarios extensively 'cause it's hard to predict how complex control will behave.