tags:

views:

78

answers:

2

Is it possible to wrap the old System.Windows.Forms controls in System.Windows.UIElement? I know that the Browser Control is somehow wrapped and the base is from System.Windows.Forms.

If this is possible, would the implementation cause any consequences?

A: 

There is the WindowsFormsHost class, though I would add a note of caution. If you're using all your old controls from winforms, mixed with WPF, it won't be a nice experience for the user. I assume you've been told you can't, or don't have time, but really you should look to replacing your existing controls with WPF controls. Unless you have lots of seriously complicated owner-drawn stuff, this shouldn't be too much effort.

So my recommendation would be to start creating WPF versions of your existing controls (or buy a set from someone like Telerik for any non-domain-specific controls you've created, like toolbars etc), and only keep Winforms controls for extra-complicated bespoke controls you've created. Even then, you should be planning for a "phase 2" to replace those as well. Your users will thank you for it.

Neil Barnwell
What issues have you found with including WinForms controls in WPF windows? I've heard on and off that people will experience problems, but in the mixed WPF/WinForms stuff I've coded there hasn't been any real difficulty combining the two. Of course all the integration I did was planned with an understanding of air space and focus. Perhaps I just avoided some obvious pitfalls without being conscious of it, or perhaps I just got lucky.
Ray Burns
Well, really I'm referring to the look-and-feel. Unless an amount of care is paid, the look-and-feel of the winforms controls will be jarring against the wpf stuff.
Neil Barnwell
+1  A: 

You can host a Windows forms control in your WPF forms. Just wrap it inside a WindowsFormsHost element. This shows how to host a windows forms masked test box in side a WPF window.

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
    Title="HostingWfInWpf"
    >

  <Grid>

    <WindowsFormsHost>
      <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
    </WindowsFormsHost>

  </Grid>


</Window>
amazedsaint