There are various ways to call a method from the code-behind.
- approach:
public partial class MainWindow : Window { public MainWindow() { ViewModel vm = new ViewModel(); this.DataContext = vm; InitializeComponent(); vm.Init(); } }
- approach:
public MainWindow() { this.Resources.Add("vm", new ViewModel()); this.Loaded += MainWindow_Loaded; InitializeComponent(); } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { var vm = (ViewModel)(this.Resources["vm"]); vm.Init(); }