private void OnLoadedInternal(object sender, RoutedEventArgs e)
        {
            if (Equals(WindowChrome.GetWindowChrome(this), _chrome) && _previousSizeToContent != SizeToContent.Manual)
            {
                SizeToContent          = _previousSizeToContent;
                _previousSizeToContent = SizeToContent.Manual;

                if (WindowStartupLocation == WindowStartupLocation.CenterScreen)
                {
                    Left = SystemParameters.VirtualScreenLeft + SystemParameters.PrimaryScreenWidth / 2 - ActualWidth / 2;
                    Top  = SystemParameters.VirtualScreenTop + SystemParameters.PrimaryScreenHeight / 2 - ActualHeight / 2;
                }
                if (WindowStartupLocation == WindowStartupLocation.CenterOwner)
                {
                    if (Owner != null)
                    {
                        if (Owner.WindowState == WindowState.Maximized)
                        {
                            var source = PresentationSource.FromVisual(Owner);
                            if (source != null && source.CompositionTarget != null)
                            {
                                var ownerHandle = new WindowInteropHelper(Owner).EnsureHandle();
                                var ownerWindow = new Native.Window(ownerHandle);
                                ownerWindow.Invalidate();
                                Left = -ownerWindow.NonClientBorderWidth * source.CompositionTarget.TransformFromDevice.M11;
                                Top  = -ownerWindow.NonClientBorderHeight * source.CompositionTarget.TransformFromDevice.M22;
                            }
                            else
                            {
                                Left = 0;
                                Top  = 0;
                            }
                        }
                        else
                        {
                            Left = Owner.Left;
                            Top  = Owner.Top;
                        }
                        Left += Owner.ActualWidth / 2 - ActualWidth / 2;
                        Top  += Owner.ActualHeight / 2 - ActualHeight / 2;
                    }
                }

                UpdateNonClientBorder();

                if (_dispatcherFrame != null)
                {
                    _dispatcherFrame.Continue = false;
                }
            }
        }
        private void UpdateNonClientBorder()
        {
            if (!Equals(WindowChrome.GetWindowChrome(this), _chrome) || _layoutRoot == null || _window == null)
            {
                return;
            }

            Taskbar.Invalidate();
            if (WindowState == WindowState.Maximized && !Taskbar.AutoHide && SizeToContent == SizeToContent.Manual)
            {
                _window.Invalidate();
                _layoutRoot.Margin = new Thickness(_window.NonClientBorderWidth, _window.NonClientBorderHeight, _window.NonClientBorderWidth, _window.NonClientBorderHeight);
            }
            else
            {
                _layoutRoot.Margin = new Thickness();
            }
        }
        private void OnLoadedInternal(object sender, RoutedEventArgs e)
        {
            if (Equals(WindowChrome.GetWindowChrome(this), _chrome) && _previousSizeToContent != SizeToContent.Manual)
            {
                SizeToContent = _previousSizeToContent;
                _previousSizeToContent = SizeToContent.Manual;

                if (WindowStartupLocation == WindowStartupLocation.CenterScreen)
                {
                    Left = SystemParameters.VirtualScreenLeft + SystemParameters.PrimaryScreenWidth / 2 - ActualWidth / 2;
                    Top = SystemParameters.VirtualScreenTop + SystemParameters.PrimaryScreenHeight / 2 - ActualHeight / 2;
                }
                if (WindowStartupLocation == WindowStartupLocation.CenterOwner)
                {
                    if (Owner != null)
                    {
                        if (Owner.WindowState == WindowState.Maximized)
                        {
                            var source = PresentationSource.FromVisual(Owner);
                            if (source != null && source.CompositionTarget != null)
                            {
                                var ownerHandle = new WindowInteropHelper(Owner).EnsureHandle();
                                var ownerWindow = new Native.Window(ownerHandle);
                                ownerWindow.Invalidate();
                                Left = -ownerWindow.NonClientBorderWidth * source.CompositionTarget.TransformFromDevice.M11;
                                Top = -ownerWindow.NonClientBorderHeight * source.CompositionTarget.TransformFromDevice.M22;
                            }
                            else
                            {
                                Left = 0;
                                Top = 0;
                            }
                        }
                        else
                        {
                            Left = Owner.Left;
                            Top = Owner.Top;
                        }
                        Left += Owner.ActualWidth / 2 - ActualWidth / 2;
                        Top += Owner.ActualHeight / 2 - ActualHeight / 2;
                    }
                }

                UpdateNonClientBorder();

                if (_dispatcherFrame != null)
                {
                    _dispatcherFrame.Continue = false;
                }
            }
        }