Wednesday, July 4, 2012

WPF : Handling Event on View From ViewModel

1. First Create Event Handler in ViewModel 

LoginViewModel

   public event EventHandler LoginCompleted = delegate { };
   public void OnLoginCompleted()
   {
        LoginCompleted(this, new EventArgs());
   }

2. Handle ViewModel Event in View


LoginView.Xaml.cs

 
public LoginView()
{
   this.DataContext = new LoginViewModel(); 
   (this.DataContext as LoginViewModel).LoginCompleted += 
              new EventHandler(LoginViewModel_LoginCompleted); 
}
void LoginViewModel_LoginCompleted(object sender, EventArgs e)
{
   //your Coode goes here         
}

No comments:

Post a Comment