MVVM-Light => Pass Command Parameter AND EventArgs to Command
Asked Answered
T

1

9

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?

Tresa answered 9/7, 2012 at 9:48 Comment(3)
is this even possible using MVVM-Light toolkit?Tresa
I would create a lightweight object that contains the information I need from the event args and the object I was normally going to send as the command parameter and just use that new lightweight object as my command parameter.Gerbold
@StewartSweet Sorry but how can you create such an object in view? can you kindly show an example?Tresa
T
11

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
    }
Tresa answered 9/7, 2012 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.