Windows 窗体 ElementHost 承载一个 wpf 菜单用户控件,该控件在 Windows 窗体主机上打开窗

本文关键字:控件 Windows 窗体 主机 开窗 用户 wpf ElementHost 一个 菜单 | 更新日期: 2023-09-27 18:36:47

我知道这可能看起来有点令人困惑,但是我有一个在Windows窗体中内置的应用程序,该应用程序在树视图中有一个菜单和近20个选项,可以在主窗体上打开Windows窗体子项,但它看起来很糟糕,所以我想在WPF中构建一个具有选项和情节提要的用户控件, 但我的问题是,当我单击 WPF 用户控件中的相应选项时,我找不到在主菜单上打开子窗体的方法

有人知道这是可能的吗?

        ElementHost host = new ElementHost();
        UserControl2 uc1 = new UserControl2();
        host.Child = uc1;
        host.Dock = DockStyle.Fill;
        this.Controls.Add(host);

Windows 窗体 ElementHost 承载一个 wpf 菜单用户控件,该控件在 Windows 窗体主机上打开窗

我用这个逻辑做到了

        public FormParent parent { get; set; }
    private void button_Click(object sender, RoutedEventArgs e)
    {
        var source = (HwndSource)PresentationSource.FromDependencyObject(button);
        var host = (System.Windows.Forms.Integration.ElementHost)System.Windows.Forms.Control.FromChildHandle(source.Handle);
        var form = (System.Windows.Forms.Form)host.TopLevelControl;
        FormChild newMDIChild = new FormChild();
        newMDIChild.MdiParent = form;
        newMDIChild.Show();