Passing a ViewModel as a Parameter in a Command

4 posts, 0 answers
  1. Saj
    Saj avatar
    2 posts
    Member since:
    Jun 2019

    Posted 13 Jun Link to this post

    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

     

     

  2. Lance | Principal TSE
    Admin
    Lance | Principal TSE avatar
    1043 posts

    Posted 13 Jun Link to this post

    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
  3. Saj
    Saj avatar
    2 posts
    Member since:
    Jun 2019

    Posted 13 Jun in reply to Lance | Principal TSE Link to this post

    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.

  4. Lance | Principal TSE
    Admin
    Lance | Principal TSE avatar
    1043 posts

    Posted 13 Jun Link to this post

    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
Back to Top