Call Command from Code Behind

前端 未结 6 1101
轻奢々
轻奢々 2020-12-23 19:03

So I\'ve been searching around and cannot find out exactly how to do this. I\'m creating a user control using MVVM and would like to run a command on the \'Loaded\' event.

相关标签:
6条回答
  • 2020-12-23 19:15

    Try this:

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        //Optional - first test if the DataContext is not a MyViewModel
        if( !this.DataContext is MyViewModel) return;
        //Optional - check the CanExecute
        if( !((MyViewModel) this.DataContext).MyCommand.CanExecute(null) ) return;
        //Execute the command
        ((MyViewModel) this.DataContext).MyCommand.Execute(null)
    }
    
    0 讨论(0)
  • 2020-12-23 19:21

    I have a more compact solution that I want to share. Because I often execute commands in my ViewModels, I got tired of writing the same if statement. So I wrote an extension for ICommand interface.

    using System.Windows.Input;
    
    namespace SharedViewModels.Helpers
    {
        public static class ICommandHelper
        {
            public static bool CheckBeginExecute(this ICommand command)
            {
                return CheckBeginExecuteCommand(command);
            }
    
            public static bool CheckBeginExecuteCommand(ICommand command)
            {
                var canExecute = false;
                lock (command)
                {
                    canExecute = command.CanExecute(null);
                    if (canExecute)
                    {
                        command.Execute(null);
                    }
                }
    
                return canExecute;
            }
        }
    }
    

    And this is how you would execute command in code:

    ((MyViewModel)DataContext).MyCommand.CheckBeginExecute();
    

    I hope this will speed up your development just a tiny bit more. :)

    P.S. Don't forget to include the ICommandHelper's namespace too. (In my case it is SharedViewModels.Helpers)

    0 讨论(0)
  • 2020-12-23 19:25

    Hi to call a command in code behind you can use this code line

    e.g.: Call a Button command

    Button.Command?.Execute(Button.CommandParameter);
    
    0 讨论(0)
  • 2020-12-23 19:27

    Preface: Without knowing more about your requirements, it seems like a code smell to execute a command from code-behind upon loading. There has to be a better way, MVVM-wise.

    But, if you really need to do it in code behind, something like this would probably work (note: I cannot test this at the moment):

    private void UserControl_Loaded(object sender, RoutedEventArgs e)     
    {
        // Get the viewmodel from the DataContext
        MyViewModel vm = this.DataContext as MyViewModel;
    
        //Call command from viewmodel     
        if ((vm != null) && (vm.MyCommand.CanExecute(null)))
            vm.MyCommand.Execute(null);
    } 
    

    Again - try to find a better way...

    0 讨论(0)
  • 2020-12-23 19:30

    Well, if the DataContext is already set you could cast it and call the command:

    var viewModel = (MyViewModel)DataContext;
    if (viewModel.MyCommand.CanExecute(null))
        viewModel.MyCommand.Execute(null);
    

    (Change parameter as needed)

    0 讨论(0)
  • 2020-12-23 19:31

    You also might have embedded your code in any MessaginCenter.Subscribe and work with MessagingCenter model. If you intend only execute something from code behind instead of clicking in a view button with Command property, it worked perfectly to me.

    I hope it helps someone.

    0 讨论(0)
提交回复
热议问题