Tuesday, May 3, 2011

WPF WindowsFormsHost VS2008 Toolbox has grayed out all the Windows Forms controls

I'm trying to use WindowsFormsHost in a WPF app so I can use some Windows Forms components.

<Grid>
    <WindowsFormsHost>

    </WindowsFormsHost>
</Grid>

This all works ok with no errors, but when I go to drag a Windows Forms component from the toolbox in VS2008, they're all grayed out. In a way this makes sense as only the WPF components are available. But how do you drop a Windows Forms component onto a WindowsFormsHost if they're all grayed out and unavailable?

From stackoverflow
  • I don't think WPF designer supports Windows Forms components. You need to specify these in the XAML or in code. A simple example of how to add a WinForms button into a WPF application. Note that the button isn't visible on the design surface.

    <Window x:Class="DataTemplateBind.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="Window1" Height="300" Width="300">
    
        <WindowsFormsHost Height="200" Name="windowsFormsHost1" Width="258" >
            <forms:Button Text="Waaa :("></forms:Button>
        </WindowsFormsHost>
    </Window>
    

    Note the added xmlns:forms namespace which isn't there by default.

    Mitch : Thanks Mikko, that did it. I was at the end of a long day and the frustration was setting in!

0 comments:

Post a Comment