WPF: What is between the Initialized and Loaded event?
Alternatively to storing a boolean flag, you can use an extension method and delegate wrapping to fake Loaded
only firing once.
public static void OnLoadedOnce(
this UserControl control,
RoutedEventHandler onLoaded)
{
if (control == null || onLoaded == null)
{
throw new ArgumentNullException();
}
RoutedEventHandler wrappedOnLoaded = null;
wrappedOnLoaded = delegate(object sender, RoutedEventArgs e)
{
control.Loaded -= wrappedOnLoaded;
onLoaded(sender, e);
};
control.Loaded += wrappedOnLoaded;
}
...
class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
this.OnLoadedOnce(this.OnControlLoaded /* or delegate {...} */);
}
private void OnControlLoaded(object sender, RoutedEventArgs e)
{
}
}
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
(source: microsoft.com)