Monday, June 4, 2012

Maximize a WPF Window with a custom window chrome and multiple monitors, accounting for the taskbar

A WPF window with WindowStyle set to None does not take the task bar height into account when maximized. This workaround will allow the window to be maximized to the correct size with a multiple monitor configuration. 


public MainWindow()
{
    InitializeComponent();
    LocationChanged += new EventHandler(MainWindow_LocationChanged);
}

void MainWindow_LocationChanged(object sender, EventArgs e)
{
    Screen current = Screen.FromHandle(new WindowInteropHelper(this).Handle);
    if(current.Primary) MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
    else MaxHeight = double.PositiveInfinity;
}