tags:

views:

22

answers:

1

I want to run some code when the Window or Control is first displayed. I can't use Loaded because that can fire more than once. I can't use Initialized because that is done by the constructor.

Is there an event somewhere between?

+1  A: 

Unfortunately there is no such event. You can use a boolean in the Loaded Method to make sure your stuff only fires once -

if(!IsSetUp)
{
   MySetUpFunction();
   IsSetUp = true;
}

Check out the WPF Windows lifetime events here:

http://msdn.microsoft.com/en-us/library/ms748948.aspx#Window_Lifetime_Events

alt text

brendan
Well that's stupid, I shouldn't have to use guard clauses like that. But it's the right answer so here's your point.
Jonathan Allen
Agreed - you could create your own window class inheriting from window and implement an event across your whole project if it's something your doing frequently.
brendan