简体   繁体   English

如何将Windows窗体面板添加到Wpf TabControl

[英]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. 我在WPF应用程序中调用一个API,它返回一个Windows Forms Panel。 My job is to display the panel inside my TabControl 我的工作是在TabControl中显示面板

At the moment, I'm doing the code in the CodeBehind in my WPF project, which looks like 目前,我正在WPF项目的CodeBehind中执行代码,

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) ! 程序运行了,没有崩溃,但是Panel没有显示在我的TabControl中,但是,TabControl确实呈现出好像有1个项目(尽管Panel的visible属性设置为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. 然后它可以按预期的方式工作(我看到了TextBlock控件),因此问题是由于Wpf不喜欢Panel而引起的。

The XAML 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>

To add windows forms controls you need to use WindowsFormHost and this is how you can add it- 要添加Windows表单控件,您需要使用WindowsFormHost ,这是您可以添加它的方式-

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, its of no use to use TabControl (rather you can directly use Grid). 另外,如果您使用的是TabControls而不是使用TabItems作为其子级,则使用TabControl毫无用处(而是可以直接使用Grid)。 But to add TabItems you should write you XAML like- 但是要添加TabItem,您应该像这样编写XAML-

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

In a probably similar scenario, we used a System.Windows.Forms.Integration.WindowsFormsHost which was added to a WPF Grid inside a WPF UserControl . 在可能类似的情况下,我们使用了System.Windows.Forms.Integration.WindowsFormsHost ,它已添加到WPF UserControl的WPF Grid 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 当我们直接将其添加到XAML中时,首先遇到了内存泄漏问题,但是能够通过在后面的代码中动态添加它来解决这些问题,例如

        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 并通过在UserControl的自定义onClose()方法中显式执行winFormHost.Dispose()

I performed a test based on your code, and I added a Winform RichTextBox in the host, it displayed in the TabControl. 我根据您的代码进行了测试,并在主机中添加了Winform RichTextBox,它显示在TabControl中。 Could you please try to add other controls in the Panel and then add the panel into TabItem? 您能否尝试在面板中添加其他控件,然后将面板添加到TabItem中? Please try to call Control.CreateControl to create the control handle before adding it into the host. 在将其添加到主机之前,请尝试调用Control.CreateControl创建控件句柄。

Please remember to mark the replies as answers if they help. 如果有帮助,请记住将答复标记为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM