viernes, 15 de octubre de 2010

Bind events within Resource Dictionary to ViewModel using Commanding

Firstly, you need to add a reference to the MVVM Light DLLs. You need two: GalaSoft.MvvmLight.dll and GalaSoft.MvvmLight.Extras.dll. In addition, you need the System.Windows.Interactivity.dll which contains the base code for all behaviors. Then, add the namespace in the resource dictionary:  




xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
Secondly, add "EventToCommand" behavior within any element that you need.
In ElementName, you can link to any other element of the page (in the above example, lstSearchResults is the name of a listbox that include this button). With DataContext, we can obtain the datacontext of any element.





/// SUMMARY
/// Command to register a new patient.
/// SUMMARY
public RelayCommand RegisterNewPatientCommand
{
 get;
 private set;
}
/// SUMMARY
/// Command to open the patient banner to view the patient details.
/// SUMMARY
public RelayCommand<PATIENT> LoadPatientBannerCommand
{
 get;
 private set;
}
In ViewModel class, you have to define the Commanding Finally, in the constructor of the viewmodel class, you need to initialize the commands:


this.RegisterNewPatientCommand = new RelayCommand(() => this.PreLoadRegisterPatientModule());
this.LoadPatientBannerCommand = new RelayCommand<PATIENT>((pat) => this.LoadPatientBanner(pat));
Source: http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx

No hay comentarios:

Publicar un comentario