5

设想

我有以下场景(我已将其简化为示例应用程序):

  • 一个 WPF 应用程序,其窗口 ( MainWindow ) 承载 WinForms UserControl ( UserControl1 )。
    • 该控件在后面的代码中动态添加到WindowsFormsHost
  • UserControl1有一个打开表单的按钮(Form1
    • 它使用form1.Show(this).

问题是:

  • Form1.Owner属性为空。
    • 在实际应用中,一些涉及.Owner属性的工作已经完成,这意味着我不能忽略这个问题。当然,理想情况下,这里不会有任何依赖。
    • 在实际应用程序中,我无法控制此代码的 WinForms 端。我们的 WPF 应用程序正在托管另一个团队的 WinForms 控件。
    • 笔记:
      • 当我改用 WinForms 主机时,该.Owner属性设置正确。
      • UserControl1在所有其他方面都很好地托管——在真实的应用程序中,其他一切都很好,只是用户控件打开的表单没有适当的所有者。

我可以理解为什么这里会出现问题,但我希望我的下一个问题的答案可能是“是”!

通过在等式的 WPF 方面进行更改,我可以做些什么来使其工作?

如果做不到这一点,在 WinForms 方面可以做些什么吗?(我可以在那里进行一些更改,这并不超出可能的范围......)

示例代码

这是我的示例应用程序中的代码。首先是WPF方面:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="700">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Name="btnAdd" Click="btnAdd_Click" Content="Add Winform"/>

        <WindowsFormsHost Grid.Row="1" Name="host" ScrollViewer.CanContentScroll="False"/>
    </Grid>
</Window>
public partial class MainWindow : Window
{
    private WindowsFormsHost host;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        UserControl1 uc1 = new UserControl1();
        WindowsFormsHost.EnableWindowsFormsInterop();
        this.host.Child = uc1;

    }
}

而现在 WinForms 方面...

UserControl1只是一个带有按钮和标签的用户控件。这是代码:

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show(this);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.label1.Text = "this: " + this + Environment.NewLine + "parent: " + this.Parent + Environment.NewLine + "toplevelcontrol: " + this.TopLevelControl;
        }
    }

Form1只是一个空表单。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        MessageBox.Show(" this: " + this + " owner: " + this.Owner);
    }
}

Owner消息框中显示的TopLevelControl和标签中显示的null在 WPF 中托管时,但在另一个 WinForms 应用程序中托管时具有值。

进一步的调查

我想这里的问题是它.Owner的类型Form,并且 WPF 应用程序中没有这种类型的实例。很难想象在这种情况下该属性可能具有什么有效价值。因此,我似乎需要更改访问Form1中的 `.Owner' 属性的代码。

4

1 回答 1

1
<Window
  xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" 
>

<my:WindowsFormsHost  Name="MapHost" ScrollViewer.CanContentScroll="False"  SizeChanged="MapHost_SizeChanged" />

MapControl 继承 System.Windows.Forms.Control

MapHost.Child = MapControl;
于 2012-12-11T20:29:32.047 回答