Richard
Richard

Reputation: 588

How to adjust the height of a window to the top of the screen?

I have a window in WPF and I need to state fit the top of the screen, the width only need 800 pixels. I have this but not working:

Title="Title" Height="Auto" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Icon="/img/icon.ico" Loaded="Window_Loaded">

I also tried this but it gives error:

Title="Title" Height="*" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Icon="/img/icon.ico" Loaded="Window_Loaded">

Does anyone know how to do it?

Upvotes: 1

Views: 567

Answers (2)

Pollitzer
Pollitzer

Reputation: 1620

Remove WindowStartupLocation="CenterScreen" from your Xaml code and insert Left="0" Top="0". You may increase Left value to your needs.

UPDATE

Misunderstanding, I didn't get that the window should take the full screen height (except task bar).

The final solution would be:

<Window ... 
        WindowStartupLocation="CenterScreen"
        Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
        Width="800" >

Upvotes: 2

vijay.k
vijay.k

Reputation: 179

You can set the height of window to screen height and then try to set the location of the window in Window_Loaded event as follows:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var CurrentWindow = (sender as Window);
    CurrentWindow.Height = SystemParameters.PrimaryScreenHeight;
    CurrentWindow.Top = 0;
    CurrentWindow.Left = SystemParameters.PrimaryScreenWidth / 2 - CurrentWindow.Width / 2;
    CurrentWindow.MaxHeight = SystemParameters.WorkArea.Bottom;

}

Upvotes: 1

Related Questions