Alex Rivera | Logout

How to restore a minimized Window in code-behind?

Asked 2011-04-03T18:24:27.230
52

This is somewhat of a mundane question but it seems to me there is no in-built method for it in WPF. There only seems to be the WindowState property which being an enum does not help since i cannot tell whether the Window was in the Normal or Maximized state before being minimized.

When clicking the taskbar icon the window is being restored just as expected, assuming its prior state, but i cannot seem to find any defined method which does that.

So i have been wondering if i am just missing something or if i need to use some custom interaction logic.

(I'll post my current solution as answer)

Edit
Report

1 Answer

3

Here is how i get it to restore right now: I handle the StateChanged event to keep track of the last state that was not Minimized

WindowState _lastNonMinimizedState = WindowState.Maximized;
private void Window_StateChanged(object sender, EventArgs e)
{
    if (this.WindowState != System.Windows.WindowState.Minimized)
    {
        _lastNonMinimizedState = WindowState;
    }
}

To restore i then have to set that WindowState respectively:

this.WindowState = _lastNonMinimizedState;
answered 2011-04-03T18:24:44.937

Your Answer