I'm working on an application c# WPF. I have my own title bar and I coded function close, minimize and maximize. I'm working on PC with 2 screens and I have a problem when I maximize window. On main screen, If I set WindowState = WindowState.Maximized
the task bar (the bar from windows 7) is hidden. I added a function to set MaxHeight depending on screen but when I change MaxHeight, the ActualHeight doesn't change. Concretely, when I maximize window, the window goes full screen. The second time I maximize, the height is correct, the task bar is not hidden.
What can I do to refresh height when changing MaxHeight ?
Here is my code :
protected override void OnStateChanged(EventArgs e)
{
base.OnStateChanged(e);
if (WindowState == WindowState.Maximized)
{
var hwnd = new System.Windows.Interop.WindowInteropHelper(this).EnsureHandle();
var currentMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST);
var primaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMERTY);
var isInPrimary = currentMonitor == primaryMonitor;
if (isInPrimary)
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
else
this.MaxHeight = Double.PositiveInfinity;
#if DEBUG
var aH = this.ActualHeight;
var h = this.Height;
var mH = this.MaxHeight;
if (aH != h || aH > mH)
{
System.Diagnostics.Debugger.Break();
this.Height = this.MaxHeight;
}
#endif
}
}