question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maximizing MetroWindow doesn't work with AllowsTransparency="True"

See original GitHub issue

Describe the bug When MetroWindow has AllowsTransparency="True" then maximizing it via click on Maximize button, double-click on titlebar, or context menu Maximize click doesn’t work.

To Reproduce Steps to reproduce the behavior:

  1. Create MetroWindow with AllowsTransparency="True"
  2. Try to maximize the window

Expected behavior The window is properly maximized.

Screenshots https://i.imgur.com/wPIaKrA.png This is how the window looks after an attempt to maximize (window content is in top left corner, but it doesn’t respond to anything except dragging by the titlebar to remove maximization).

Environment(please complete the following information):

  • MahApps.Metro version v1.6.5, v2.0.0-alpha0660
  • OS: Win10 1909
  • .NET Framework 4.6.1, .NET Core 3.0

Repo

I’ve created the repo to follow the guidelines https://github.com/skel35/MetroWindowMaximizeBugRepro, but the repro is very simple:

<mah:MetroWindow x:Class="NetCoreRepro.MainWindow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
                 mc:Ignorable="d"
                 Title="MainWindow" Height="350" Width="525"
                 AllowsTransparency="True"
                 Background="White">
    <Grid />
</mah:MetroWindow>

Additional context

Maximizing works properly if I drag the titlebar to the top edge of the screen.

I’d appreciate if you can recommend any workaround to this issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mxsoftwarecommented, Jan 28, 2020

Goodmorning everyone, first of all, congratulations for the enormous work that you are carrying out.

Also I had problems with “AllowsTransparency”, I solved, surely in a non elegant and completed way, with the following code:

public class BaseWindow : MahApps.Metro.Controls.MetroWindow

...

#region " private vars "
        private static bool _ManageWpfAllowsTrasparencyBug = true;

        private bool _FirstEnteredOnActivated;
        private object _OldDataContext;

        private bool _DisableStoreWindowCoordinates;
        private double? _Top;
        private double? _Left;
        private double? _Height;
        private double? _Width;
        private WindowState _PreviousWindowState;
#endregion

...

#region " constructors "
        public MxBaseWindow()
        {
            if (_ManageWpfAllowsTrasparencyBug)
            {
                _PreviousWindowState = this.WindowState;

                this.SizeChanged += BaseWindow_SizeChanged;
                this.StateChanged += BaseWindow_StateChanged;
            }
        }
#endregion

#region " methods "
        private void BaseWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (!_DisableStoreWindowCoordinates)
            {
                _Top = Top;
                _Left = Left;
                _Height = Height;
                _Width = Width;
            }
        }

        private void BaseWindow_StateChanged(object sender, EventArgs e)
        {
            if ((this.WindowState == WindowState.Maximized) && (_PreviousWindowState == WindowState.Maximized))
            {
                if (_Top.HasValue)
                {
                    _DisableStoreWindowCoordinates = true;

                    _PreviousWindowState = WindowState.Normal;

                    this.StateChanged -= MxBaseWindow_StateChanged;
                    this.WindowState = WindowState.Normal;
                    this.StateChanged += MxBaseWindow_StateChanged;

                    this.Top = this._Top.Value;
                    this.Left = this._Left.Value;
                    this.Height = this._Height.Value;
                    this.Width = this._Width.Value;

                    _DisableStoreWindowCoordinates = false;
                }
            }
            else if ((this.WindowState == WindowState.Maximized) && (_PreviousWindowState != WindowState.Maximized))
            {
                _DisableStoreWindowCoordinates = true;

                _PreviousWindowState = this.WindowState;

                this.StateChanged -= MxBaseWindow_StateChanged;
                this.WindowState = WindowState.Normal;
                this.StateChanged += MxBaseWindow_StateChanged;

                this.Top = 0;
                this.Left = 0;
                this.Width = SystemParameters.WorkArea.Width;
                this.Height = SystemParameters.WorkArea.Height;

                _DisableStoreWindowCoordinates = false;
            }
            else
            {
                _PreviousWindowState = this.WindowState;
            }
        }
#endregion
0reactions
mxsoftwarecommented, Jan 25, 2020

If the question is addressed to me, I don’t use WindowsFormHost. I don’t like mixing different technologies. The project from which I extracted the code is native WPF with MahApps.Metro 1.6.5. I have not found any stability problems, but as already highlighted I have no experience with the WindowsFormHost control.

Read more comments on GitHub >

github_iconTop Results From Across the Web

wpf - MahApps Window maximizing doesn't work when ...
I have experienced a bit a weird behavior of the MahApps Window when I set the AllowsTransparency to true. What I am doing...
Read more >
Quick Start
For now we'll use MetroWindow , as this approach will work for a good percentage of apps and is the quickest and easiest...
Read more >
Windows in WPF overview - WPF .NET
Learn about the basics of how Window objects work in WPF. Learn how to create and manage a window for a Windows Presentation...
Read more >
Newest 'elementhost' Questions - Stack Overflow
I'm trying to use an elementhost in my child form but it distorts my parent form. Tried to refresh and minimize/maximize the parent...
Read more >
Mouse Resizng not working when AllowsTransparency=" ...
I have a WPF application, by default the resizing perfectly work. but as soon as I set the AllowsTransparency="True" the mouse resizing from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found