Search code examples
c#wpfwinformstablelayoutpanelwindowsformshost

XAML-embedded WinForms Panel doesn't support direct content?


I'm attempting to place a Winforms panel within my WPF UserControl, with the following code;

<WindowsFormsHost Grid.Row="3">
    <WinForms:Panel>
        <WinForms:TableLayoutPanel x:Name="myLayoutPanel" />
    </WinForms:Panel>
</WindowsFormsHost>

Error:

The type 'Panel' does not support direct content.

I'm will then then initialise the TableLayoutPanel panel within C# code. Any ideas how I can workaround this error?


Solution

  • the Windows.Forms Panel Container is called Controls. You should be able to add it by doing something like this: If it were me I would just create a Winforms UserControl and add that to the WinFormsHost instead.

    <WindowsFormsHost Height="100" HorizontalAlignment="Left" Margin="10,108,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="200">
        <WinForms:Panel BackColor="Red" Dock="Fill">
            <WinForms:Panel.Controls>
                <WinForms:TableLayoutPanel x:Name="myLayoutPanel"/>
            </WinForms:Panel.Controls>
        </WinForms:Panel>
    </WindowsFormsHost>