Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM-Light => Pass Command Parameter AND EventArgs to Command

Using MVVM-Light Toolkit in Silverlight 5, i am trying to find a way to pass Command Parameters AND EventArgs both to ViewModel in an Event-To-Command behavior.

I did find a post suggesting Passing EventArgs as Command Parameters but in my case i want to use EventArgs and Command Parameter both in ViewModel.

Anyone can help?

like image 602
Thr3e Avatar asked Jul 09 '12 09:07

Thr3e


People also ask

How do you pass a command parameter in WPF MVVM?

A parameter can be passed through the "CommandParameter" property. Once the button is clicked the selected address value is passed to the ICommand. Execute method. The CommandParameter is sent to both CanExecute and Execute events.

What is Mvvm command?

Commands are an implementation of the ICommand interface that is part of the . NET Framework. This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.


1 Answers

Solved the Issue .... in case if anyone else is wondering ...

Concept : We only need to pass the EventArgs via MVVM-Light Event to Command. In event to Command, there is a property Source. we can cast this 'Source' Property to the object that generated this command.

Example :

we declare command with eventargs in ViewModel Constructor

FilterQuotationsCommand = new RelayCommand<GridViewFilteredEventArgs>(FilterQuotationsCommandExecute);

And we Access the Sender via the "Source" after casting it to the sending control.

private void FilterQuotationsCommandExecute(GridViewFilteredEventArgs e)
    {
        var grid = (RadGridView) e.Source; // we casted the Source to Grid
        var item = grid.SelectedItem;      // we can access grid's selected items
    }
like image 116
Thr3e Avatar answered Oct 12 '22 22:10

Thr3e