MyDaftQuestions
MyDaftQuestions

Reputation: 4691

How to add Windows Form Panel to Wpf TabControl

I have been given a task and I'm stumped. I call an API in my WPF application, and it returns a Windows Forms Panel. My job is to display the panel inside my TabControl

At the moment, I'm doing the code in the CodeBehind in my WPF project, which looks like

public MainWindow()
{
    InitializeComponent();
    var panel = GetPanelFromApi();        

    this.MyTabControl.Items.Add(panel);//fail
}

The programs runs, no crash, but the Panel isn't shown in my TabControl, however, the TabControl does render as if there is 1 item (may it just isn't visible despite the panel's visible property set to true) !

If I update the code to

public MainWindow()
{
    InitializeComponent();
    TextBlock tb = new TextBlock();
    tb.Text = "Test";

    this.MyTabControl.Items.Add(tb);//yipee
}

Then it works as expected (I see the TextBlock control) so the issue is due to the Wpf not liking the Panel... I don't know how to fix it or what I should be looking at.

The XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
          <TabControl x:Name="MyTabControl"></TabControl>        
    </Grid>
</Window>

Upvotes: 1

Views: 5829

Answers (3)

Rohit Prakash
Rohit Prakash

Reputation: 1972

To add windows forms controls you need to use WindowsFormHost and this is how you can add it-

    System.Windows.Forms.Integration.WindowsFormsHost host1 =
        new System.Windows.Forms.Integration.WindowsFormsHost();

    System.Windows.Forms.Panel panel =
        new System.Windows.Forms.Panel()
        {
            BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle};
            host1.Child = panel;

            ((this.MyTabControl.Items[0] as TabItem).Content as Grid).Children.Add(host1);

Also, If you are using TabControls and not using TabItems as its children, it's of no use to use TabControl (rather you can directly use Grid). But to add TabItems you should write you XAML like-

    <TabControl x:Name="MyTabControl" >
        <TabItem Header="TabItem 1">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
        <TabItem Header="TabItem 2">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
    </TabControl>

Upvotes: 5

Stefan Wuebbe
Stefan Wuebbe

Reputation: 2149

In a probably similar scenario, we used a System.Windows.Forms.Integration.WindowsFormsHost which was added to a WPF Grid inside a WPF UserControl. We got memory leak issues first when we added it directly in the XAML, but were able to resolve those by adding it dynamically in the code behind, something like

        winFormsHost = new System.Windows.Forms.Integration.WindowsFormsHost(); // WindowsFormsHost not added in Xaml in order to avoid Memory Leak
        namedWpfGrid.Children.Add(winFormsHost);

        dbiScheduler = new Dbi.WinControl.Schedule.dbiSchedule();
        dbiScheduler.FirstDraw += ... // lots of event-handler and property assignments following here
        winFormsHost.Child = dbiScheduler;

And by explicitly doing a winFormHost.Dispose() in a custom onClose() method of the UserControl

Upvotes: 2

Bhavik Soni
Bhavik Soni

Reputation: 186

I performed a test based on your code, and I added a Winform RichTextBox in the host, it displayed in the TabControl. Could you please try to add other controls in the Panel and then add the panel into TabItem? Please try to call Control.CreateControl to create the control handle before adding it into the host.

Please remember to mark the replies as answers if they help.

Upvotes: 1

Related Questions