Poma
Poma

Reputation: 8484

Window shown event in WPF?

I want to apply fade animation every time my window is shown. How to do that from xaml? That window can be hidden and then shown again so I can't use Loaded event.

Upvotes: 36

Views: 55559

Answers (2)

MiiChiel
MiiChiel

Reputation: 231

You could use the

IsVisibleChanged

Event from the WPF Window;

Then in the EventMethod use:

if((bool)e.IsVisible)
{
   // It became visible
}
else
{
  // It became hidden
}

This works with opening a new Window instance, this.Show(), this.hide(), this.Close()

Upvotes: 7

ezolotko
ezolotko

Reputation: 1783

You can use the ContentRendered event or override OnContentRendered virtual method like this:

    bool _shown;

    protected override void OnContentRendered(EventArgs e)
    {
        base.OnContentRendered(e);

        if (_shown)
            return;

        _shown = true;

        // Your code here.
    }

Upvotes: 59

Related Questions