This is a migrated thread and some comments may be shown as answers.

Passing a ViewModel as a Parameter in a Command

3 Answers 1118 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Saj
Top achievements
Rank 1
Saj asked on 13 Jun 2019, 01:49 PM

Hi all

I have been looking at the example (App7), specifically this line:

 <commands:DataGridUserCommand Id="CommitEdit" Command="{Binding Source={StaticResource ViewModel}, Path=MyCommand}" />

I am trying to pass my ViewModel as a parameter so I can access it here:

 private void OnMyCommandExecute(object obj)

At the moment, the Parameter property is empty but CellInfo is populated.

I hope my question is clear.

Thanks

 

 

3 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 13 Jun 2019, 03:48 PM
Hi Saj,

That method should already be inside the view model, so you do not need to pass the view model reference as a parameter.:

private void OnMyCommandExecute(object obj)
{
    var viewModel = this;
}


However, if you're referring to writing a custom command class, that Execute method would be inside the class. Even still, you do not need to send the view model as a command parameter.

Instead, you can get a reference to the view model using the DataGrid's DataContext property:

public class CustomCommitEditCommand : DataGridCommand
{
    public CustomCommitEditCommand()
    {
        this.Id = CommandId.CommitEdit;
    }
 
    public override bool CanExecute(object parameter)
    {
        return true;
    }
 
    public override void Execute(object parameter)
    {
        var context = parameter as EditContext;
 
        var viewModel = this.Owner.DataContext as MainViewModel;
 
 
        this.Owner.CommandService.ExecuteDefaultCommand(CommandId.CommitEdit, context);
    }
}


If you have any trouble, please prepare a test project that I can directly investigate. Since forum posts don't allow you to attach a ZIP file, you could instead host it (OneDrive, DropBox, etc) and send me a download link.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Saj
Top achievements
Rank 1
answered on 13 Jun 2019, 04:31 PM

By using an ICommand that is decoupled from the View, I would like the individual row ViewModel passed as a Parameter as this is consistent with the principle of dependency injection.

However, at the moment, the Parameter property of obj is always NULL.

Thanks.

0
Lance | Manager Technical Support
Telerik team
answered on 13 Jun 2019, 04:42 PM
Hi Saj,

If you're referring the row's data item as the ViewModel and not the page's ViewModel, then that reference is available via the EditContext context object passed to the command.

var context = obj as Telerik.UI.Xaml.Controls.Grid.Commands.EditContext;
var dataItem = context.CellInfo.Item;

If this not what you're seeing and obj is null, then something else is wrong. If that is still the case, please provide the rest of the code that I can use to replicate the problem.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
DataGrid
Asked by
Saj
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Saj
Top achievements
Rank 1
Share this question
or