C# WPF: How to maximize a window when it's dragged to the top of the screen

2.2k views Asked by At

So I have a WPF application that maximizes window when it's dragged to the top of the screen.

However, I'd like to change a property, and because of that I think it would be best If I create my own drag maximize properties.

What's the easiest way to do that?

Thanks in advance.

1

There are 1 answers

1
Bharat Mallapur On

You can check if the WindowState of your window is set to "Maximized". If it is maximized, you can change the text accordingly.

For this, you need to subscribe to the SizeChanged event of the window, and in the event handler, check if the WindowState is set to Maximized/Normal. If so, you can change the text accordingly.

I'm assuming that you're using a custom window trying to denote the minimize, restore and close buttons using buttons with font "Wingdings" or some such font which has glyphs to denote the icons for minimize, restore/maximize, and close.

Anyway, even if my assumption is wrong, you can always adapt the code below as per your situation.

    public CustomWindow()
    {
        SizeChanged += CustomWindow_SizeChanged;
    }

    void CustomWindow_SizeChanged(object sender, SizeChangedEventArgs e)
    {
         CheckRestoreButtonIcon();
    }

    protected void CheckRestoreButtonIcon()
    {          
        //i'm assuming that the button is named as restoreButton.
        //in wingdings, 1 is for maximized glyph, 2 is for restore glyph
        // you can always set content to whatever you want!

        if (restoreButton == null)
            return;

        if (WindowState == WindowState.Maximized)
            restoreButton.Content = "1"; //maximizee glyph
        else
            restoreButton.Content = "2";//restore glyph
    }