Example #1
0
        public bool TrySetWindowOwner(DependencyObject source, Window target)
        {
            bool result = false;

            Fx.Assert(target != null, "Target window cannot be null");
            if (null != target)
            {
                if (source != null)
                {
                    //try the easy way first
                    Window owner = Window.GetWindow(source);
                    if (null != owner)
                    {
                        target.Owner = owner;
                        result       = true;
                    }
                }
                //no - it didn't work
                if (!result)
                {
                    IntPtr ownerHwnd = Win32Interop.GetActiveWindow();
                    if (ownerHwnd == IntPtr.Zero)
                    {
                        ownerHwnd = this.ParentWindowHwnd;
                    }
                    WindowInteropHelper interopHelper = new WindowInteropHelper(target);
                    interopHelper.Owner = ownerHwnd;
                    result = true;
                }
            }

            return(result);
        }