Is there a “All Children loaded” event in WPF

后端 未结 7 966
陌清茗
陌清茗 2020-12-13 02:49

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

7条回答
  •  孤城傲影
    2020-12-13 03:11

    I ended up doing something along these lines.. your milage may vary.

    void WaitForTheKids(Action OnLoaded)
    {
      // After your children have been added just wait for the Loaded
      // event to fire for all of them, then call the OnLoaded delegate
    
      foreach (ContentControl child in Canvas.Children)
      {
        child.Tag = OnLoaded; // Called after children have loaded
        child.Loaded += new RoutedEventHandler(child_Loaded);
      }
    }
    internal void child_Loaded(object sender, RoutedEventArgs e)
    {
      var cc = sender as ContentControl;
      cc.Loaded -= new RoutedEventHandler(child_Loaded);
    
      foreach (ContentControl ctl in Canvas.Children)
      {
        if (!ctl.IsLoaded)
        {
          return;
        }
      }
      ((Action)cc.Tag)();
    }
    

提交回复
热议问题