Custrom 控件完全显示后的 C# WPF 触发事件



我正在尝试弄清楚如何在显示控件后触发事件。例如。

    private void MainWindow_Ready(object sender, DataSet e)
    {
            foreach (table in e.Tables)
            {
                control = new LeagueControl(table);
                SP1.Children.Add(control);
            }
        });
    }
    void SomeMethod()
    {
    }

因此,在我添加到 SP1 的控件实际显示在我的窗口中之后,我想运行方法 SomeMethod((;

这样做,我能够得到我需要的东西。

            int count = 0;
            foreach (DataTable table in e.Tables)
            {
                count++;
                control = new LeagueControl(table);
                SP1.Children.Add(control);
                if (count == e.Tables.Count)
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =>
                    {
                        Console.WriteLine("X");
                    }));
                }
            }

真的,它对我有用,但有更好的方法吗?我不确定它为什么有效,也许有人可以详细说明,谢谢。

将委托函数作为 eventHandler 传递给 Loaded 事件。

法典:

control.Loaded += delegate(object o, RoutedEventArgs e){
    SomeMethod();
}

最新更新