1.

What Is The View First Construction In Code-behind?

Answer»

Another way is that you can do view first construction by simply constructing the view model yourself in the CODE BEHIND of your View by setting the DataContext property there with the instance.

Typically, the DataContext property is set in the constructor method of view, but you could ALSO DEFER the construction until the Load event of the view fires.

using System.Windows.Controls;
namespace MVVMDemo.Views { 
/// <summary> 
/// Interaction logic for StudentView.xaml 
/// </summary> 
public PARTIAL class StudentView : UserControl { 
public StudentView() { 
InitializeComponent(); 
this.DataContext = new MVVMDemo.ViewModel.StudentViewModel(); 


}

Another way is that you can do view first construction by simply constructing the view model yourself in the code behind of your View by setting the DataContext property there with the instance.

Typically, the DataContext property is set in the constructor method of view, but you could also defer the construction until the Load event of the view fires.

using System.Windows.Controls;
namespace MVVMDemo.Views { 
/// <summary> 
/// Interaction logic for StudentView.xaml 
/// </summary> 
public partial class StudentView : UserControl { 
public StudentView() { 
InitializeComponent(); 
this.DataContext = new MVVMDemo.ViewModel.StudentViewModel(); 


}



Discussion

No Comment Found