Search code examples
c#.netwpffullscreen

Video no sizing to window when coming out of fullscreen


I'm doing a media player as a WPF application using .NET.

I just did the fullscreen thingy and it's working well. But when I come out of fullscreen, if I try to resize the window after, the video playing in the window won't stretch to the window.

I checked and I put

        <MediaElement Name="MediaPlayer" Grid.Row="1" LoadedBehavior="Manual" Stretch="Fill" />

Stretch="Fill" is put, so what did I missed?

Here's my fullscreen function:

    private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
    {

        if (e.ClickCount == 2)
        {
            if (!fullscreen)
            {
                MediaPlayer.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                MediaPlayer.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
                MediaPlayer.Stretch = System.Windows.Media.Stretch.Fill;
            }
            else
            {
                MediaPlayer.Width = 1280;
                MediaPlayer.Height = 720;
                this.WindowStyle = WindowStyle.SingleBorderWindow;
                this.WindowState = WindowState.Normal;
                MediaPlayer.Stretch = System.Windows.Media.Stretch.Fill;
            }
            fullscreen = !fullscreen;
        }
    }

edit: I got this problem only if I enter in fullscreen once. I can to the resize and the video will fit to the window only if I don't put my window in fullscreen before.


Solution

  • Currently, you hardcoded MediaElement Width/Height in fullscreen method, and MediaElemet keeps the hardcoded size when you toogle fullscreen. Remove it, and it will work well:

    private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount == 2)
        {
            if (!fullscreen)
            {
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
            }
            else
            {
                this.WindowStyle = WindowStyle.SingleBorderWindow;
                this.WindowState = WindowState.Normal;
            }
            fullscreen = !fullscreen;
        }
    }