Is there a "All Children loaded" event in WPF

Viewed 15009

I am listening for the loaded event of a Page. That event fires first and then all the children fire their load event. I need an event that fires when ALL the children have loaded. Does that exist?

7 Answers

Loaded is the event that fires after all children have been Initialized. There is no AfterLoad event as far as I know. If you can, move the children's logic to the Initialized event, and then Loaded will occur after they have all been initialized.

See MSDN - Object Lifetime Events.

WPF cant provide that kind of an event since most of the time Data is determining whther to load a particular child to the VisualTree or not (for example UI elements inside a DataTemplate)

So if you can explain your scenario little more clearly we can find a solution specific to that.

Put inside your xaml component you want to wait for, a load event Loaded="MyControl_Loaded" like

<Grid Name="Main" Loaded="Grid_Loaded"...>
<TabControl Loaded="TabControl_Loaded"...>
<MyControl Loaded="MyControl_Loaded"...>
...

and in your code

bool isLoaded;

private void MyControl_Loaded(object sender, RoutedEventArgs e)
{
   isLoaded = true;
}

Then, inside the Event triggers that have to do something but were triggering before having all components properly loaded, put if(!isLoaded) return; like

private void OnButtonChanged(object sender, RoutedEventArgs e)
{
    if(!isLoaded) return;

    ... // code that must execute on trigger BUT after load
}
Related