Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Control: where is "OnLoaded" virtual function?

Tags:

wpf

controls

In WinForm's control, there is an OnLoaded virtual function, but this seems to be missing in WPF control. I found this function very useful in some situations. For example, I could do something here after the control is "completely" initialized. In WPF control, there is an OnInitialized virtual function, but this function is called from InitializeComponent function which is too early and it doesn't allow derived class to setup. Is there any reason not to have this function in WPF? Or did I miss anything?

like image 211
newman Avatar asked Jul 04 '11 07:07

newman


2 Answers

You can attach to the Loaded event of your Window object and do what you want to do inside the event handler (assuming you are using c#):

public MyWindow() //constructor
{
  this.Loaded += MyWindow_Loaded;
}

private void MyWindow_Loaded(object sender, RoutedEventArgs e)
{
  // do your stuff here
}
like image 194
Edwin de Koning Avatar answered Sep 18 '22 16:09

Edwin de Koning


you will be looking for FrameworkElement.EndInit()

This will work after the initialization process of the Element...

like image 26
Binil Avatar answered Sep 19 '22 16:09

Binil