C# WPF MVVM Window OnLoad Binding
c#, mvvm, wpf
Solution
Accessing UI component from ViewModel is violation of MVVM pattern.
`Application.Current.MainWindow.IsInitialized` is breaking that pattern.
Custom behaviours is more in accordance with MVVM. So, i would suggest to go with the approach you mentioned as link in your question.
Accessing UI component breaks the testability of your ViewModel. How would you write testcase for your ViewModel class? `Application.Current` will be `null` when you try to test it via unit test and it will throw null reference exception.
One of the main motive of MVVM was to seperate UI logic from business logic so that business logic can be tested separately without worrying about its consumer which is view.
Problem
My Code behind looks like this... ``` public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new MainWindowViewModel(); } } ``` My ViewModel looks like this... ``` class MainWindowViewModel : INotifyPropertyChanged { public MainWindowViewModel() { bool flag = Application.Current.MainWindow.IsInitialized; if (flag) { // Do something... } } ``` I guess my question is....Does this conform to the MVVM design pattern? The only other way to do this is How to fire a Command when a window is loaded in wpf I don't know why, but I don't want to use mvvm-light or any other boilerplate code..