[SOLVED] WPF doesn’t want to go full screen

Issue

This Content is from Stack Overflow. Question asked by Павел Нестеренко

Hello!
I was making my own minimize and fullscreen buttons but ran into a problem.
I have some window:

<Window x:Class="LeagueOfTeamsUI.Views.GameWindow"
    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:LeagueOfTeamsUI.Views"
    mc:Ignorable="d"
    
    x:Name="GameMainWindow" Title="GameWindow" AllowDrop="True"
    MinWidth="1440" MinHeight="810" WindowStyle="None"
    
    MouseLeftButtonDown="GameMainWindow_MouseLeftButtonDown"  Background="{x:Null}"
    SizeToContent="WidthAndHeight" AllowsTransparency="True">

And have toggle button, to make window fullscreen:

private void CheckedFullScreenButton_Click(object sender, RoutedEventArgs e)
    {
        GameMainWindow.WindowState = WindowState.Maximized;
    }
private void UncheckedFullScreenButton_Click(object sender, RoutedEventArgs e)
    {
        GameMainWindow.WindowState = WindowState.Normal;
    }
private void GameMainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.DragMove();
    }

But when I use this button – the window stays the same size. It only sticks to the top left corner.

I found that if I manually drag the window to the edge of the screen once (on Win10 this stretches the window to half the screen), then nothing changes and the window also sticks to the corner. But if you repeat this action, the window is stretched to half the screen. After that, the fullscreen button works correctly while the app is running.

Please show me what I did wrong?



Solution

Your problem is the SizeToContent property. You set it to SizeToContent="WidthAndHeight" which means that:

// Automatically resize height and width relative to content

this.SizeToContent = SizeToContent.WidthAndHeight;

While your window maximizes the content size does not change, and the window size will stay the same.

Docs:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.window.sizetocontent?view=windowsdesktop-6.0

Remove that property and it should work.


This Question was asked in StackOverflow by Павел Нестеренко and Answered by Nawed Nabi Zada It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?