C# WPF中是否有“所有孩子加载”事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/567216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:29:43  来源:igfitidea点击:

Is there a "All Children loaded" event in WPF

c#.netwpf

提问by Shaun Bowe

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?

我正在监听页面的加载事件。该事件首先触发,然后所有孩子触发他们的加载事件。我需要一个在所有孩子都加载时触发的事件。那存在吗?

采纳答案by buckley

I hear you. I also am missing an out of the box solution in WPF for this.

我听你的。为此,我还缺少 WPF 中的开箱即用解决方案。

Sometimes you want some code to be executed after all the child controls are loaded.

有时您希望在加载所有子控件后执行一些代码。

Put this in the constructor of the parent control

把它放在父控件的构造函数中

Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => {code that should be executed after all children are loaded} ));

Helped me a few times till now.

到现在为止帮了我几次。

回答by configurator

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

Loaded是在所有孩子都已经过之后触发的事件InitializedAfterLoad据我所知,没有任何事件。如果可以,把孩子的逻辑移到Initialized事件中,然后Loaded在他们全部初始化后发生。

See MSDN - Object Lifetime Events.

请参阅MSDN - 对象生命周期事件

回答by Jobi Joy

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)

WPF 无法提供这种事件,因为大部分时间 Data 正在确定是否将特定子项加载到 VisualTree 中(例如 DataTemplate 中的 UI 元素)

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

所以如果你能更清楚地解释你的场景,我们可以找到一个特定的解决方案。

回答by Weezelboy

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)();
}

回答by Tono Nam

I had the same problem. @configurator is right, if you are having this problem then I believe it is because you have a problem with your child elements. In my case the child elements where a custom user control. I was initializing them incorrectly. I fixed the user control and everything worked great

我有同样的问题。@configurator 是对的,如果您遇到此问题,那么我相信这是因为您的子元素有问题。在我的情况下,自定义用户控件所在的子元素。我错误地初始化它们。我修复了用户控件,一切都很好

回答by Rover

回答by Ievgen Naida

One of the options (when content rendered):

选项之一(当内容呈现时):

this.LayoutUpdated += OnLayoutUpdated;

this.LayoutUpdated += OnLayoutUpdated;

private void OnLayoutUpdated(object sender, EventArgs e)
            {
                if (!isInitialized && this.ActualWidth != 0 && this.ActualHeight != 0)
                {
                    isInitialized = true;
                    // Logic here
                }
            };