How to maximize window without hiding windows taskbar

Sarah 186 Reputation points
2022-03-20T11:43:55.657+00:00

I use the following code to maximize the WPF window. The problem is that the Windows taskbar is hidden.

        if (this.WindowState == WindowState.Normal)
        {
            this.WindowState = WindowState.Maximized;
        }
        else if (this.WindowState == WindowState.Maximized)
        {
            this.WindowState = WindowState.Normal;
        }

with the following additional code the problem is solved for one monitor. When using multiple monitors, the window is maximized to the size of the main monitor?

this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;

Is there a way to maximize the window to the current monitor in use?

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-03-20T17:28:08.127+00:00

    Hi Sarah,
    I cannot reproduce your problem with following code:

    XAML:

    <Window x:Class="WpfApp1.Window008"  
            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:local="clr-namespace:WpfApp008"  
            mc:Ignorable="d"  
            Title="Window008" Height="450" Width="800"  
            local:ViewModel.AttProp="True">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
        <StackPanel>  
        <Button Content="Change" Command="{Binding}" Width="200"/>  
      </StackPanel>  
    </Window>  
    

    ViewModel:

    using System;  
    using System.Windows;  
    using System.Windows.Input;  
      
    namespace WpfApp008  
    {  
      public class ViewModel : ICommand  
      {  
        public Window Wnd;  
        public event EventHandler? CanExecuteChanged;  
        public bool CanExecute(object? parameter) => true;  
        public void Execute(object? parameter)  
        {  
          if (Wnd == null) return;  
          if (Wnd.WindowState == WindowState.Normal) Wnd.WindowState = WindowState.Maximized;  
          else if (Wnd.WindowState == WindowState.Maximized) Wnd.WindowState = WindowState.Normal;  
        }  
      
        #region attached property  
        public static readonly DependencyProperty AttPropProperty =  
            DependencyProperty.RegisterAttached("AttProp", typeof(bool), typeof(ViewModel), new PropertyMetadata(false, OnPropChanged));  
        public static bool GetAttProp(DependencyObject obj) => (bool)obj.GetValue(AttPropProperty);  
        public static void SetAttProp(DependencyObject obj, bool par) => obj.SetValue(AttPropProperty, par);  
        private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
        {  
          Window w = d as Window;  
          if (w == null) return;  
          w.Loaded += (s, mbe) => ((ViewModel)(w.DataContext)).Wnd = w;  
        }  
        #endregion  
      }  
    }  
    

    Result:

    184952-x.gif


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.