Search code examples
wpfmaximize-window

C# WPF Auto Resize Window When Taskbar Height Changed


I have a WPF window which has these properties:

-ResizeMode=NoResize

-WindowStyle=None

I made every functionality of a normal window but i can't figure out how can i make window auto resize itself(when it's maximized) when taskbar's height changes. (Like Microsoft Visual Studio 2017 Window).

I can manually maximize my window but if I hide taskbar there is an empty space between my window and bottom of screen.

Is there any event fired when working area changes?


Solution

  • For your problem you can use SystemParameters.WorkArea. Initially set the MaxHeight of your MainWindow.

    MaxHeight="{Binding Height, Source={x:Static SystemParameters.WorkArea}}"
    

    Register to the SystemParameters.StaticPropertyChanged in the codebehind of the MainWindow to receive changes and update your window size.

    SystemParameters.StaticPropertyChanged += (sender, args) =>
    {
        if (args.PropertyName == nameof(SystemParameters.WorkArea))
        {
             this.Dispatcher.Invoke(() =>
             {
                 MaxHeight = SystemParameters.WorkArea.Height;
                 Height = SystemParameters.WorkArea.Height;
                 WindowState = WindowState.Normal;  // Updates the windows new sizes
                 WindowState = WindowState.Maximized;
             });
        }
    };