【问题标题】:WPF owned by WinForm has null Owner property + need to get owner locationWinForm 拥有的 WPF 具有 null Owner 属性 + 需要获取所有者位置
【发布时间】:2013-10-18 20:56:48
【问题描述】:

在 WPF 窗口的构造函数中,我使用WindowInteropHelper 将其所有者设置为 WinForm。设置后,WPF 窗口的Owner 属性仍然为空。

public WpfWindow(System.Windows.Forms.Form owner)
{
    if (owner != null)
    {
        var helper = new System.Windows.Interop.WindowInteropHelper(this);
        helper.Owner = owner.Handle;

        var Owner.Width; // Owner is null
     }
}

我需要获取父级的位置信息,希望使用Owner.Left、Owner.Width等,无论所有者是WPF Window还是WinForm。

这可能吗?如果没有,除了在我的 WPF 类中保留对 WinForm 的引用之外,我还有什么选择?

【问题讨论】:

    标签: c# .net wpf winforms


    【解决方案1】:

    Window.Owner 属性必须是要设置的Window (WPF)。

    WindowInteropHelper 允许您设置用于对话框放置等的窗口所有者,但不会设置所有者属性。

    由于您直接在方法中执行此操作,因此您可以直接使用owner 参数:

    public WpfWindow(System.Windows.Forms.Form owner)
    {
        if (owner != null)
        {
            int width = owner.Width; // Just use the parameter...
         }
    }
    

    这可能吗?如果没有,除了在我的 WPF 类中保留对 WinForm 的引用之外,我还有什么选择?

    如果您需要在构造函数之外使用它,这是一种选择。另一种选择是保留WindowInteropHelper,并使用存储在helper.Owner 属性中的HWND 上的P/Invoke 来提取适当的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 2014-10-12
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多