1

Since I started using MVVM, I've always used PRISM's DelegateCommand class for binding commands in my view models to button commands in my views. I believe Telerik also has an equivalent of DelegateCommand.

My question is, is there a built in alternative to using 3rd party frameworks such as prism and telerik. If I'm throwing together a quick throwaway application I may not want the hassle of installing packages from NuGet. Is there a way to achieve the same thing using a Func or an Action or a delegate?

  • AFAIK, there aren't any implementations already made for you in the framework. However, I personally like MVVM Foundation. You could just copy the RelayCommand class to your project, and now you have what you're asking for. Technically, it's still a 3rd party framework, but if you only need the one class, it's the same as writing it yourself: https://mvvmfoundation.codeplex.com/ – Will Eddins Feb 10 '15 at 23:06

1 Answers1

1

No, you still need a Command class that implements ICommand. However, you can write your own DelegateCommand really easily (citation, I wrote this off the top of my head in less than a minute):

public class DelegateCommand : ICommand
{
     private Action<object> execute;

     public DelegateCommand(Action<object> executeMethod)
     {
          execute = executeMethod;
     }

     public bool CanExecute(object param)
     {
         return true;
     }

     public void Execute(object param)
     {
         if (execute != null)
             execute(param);
     }
}

Use and enjoy! You could take an additional Func<bool, object> parameter if you wanted custom CanExecute behavior instead of returning true.

Note, if you really don't like null as the function, and want it to throw if you try it, just use this constructor instead:

     public DelegateCommand(Action<object> executeMethod)
     {
          if (executeMethod == null)
              throw new ArgumentNullException("executeMethod");

          execute = executeMethod;
     }
Tseng
  • 61,549
  • 15
  • 193
  • 205
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • @ConnellOD, additionally, I would suggest you to check in the `Execute()` method whether `CanExecute()` returns true and act respectively. – dymanoid Feb 13 '15 at 12:08
  • @dymanoid While there is a threading hazard there, typically `CanExecute` is used to simply disable the UI control, and isn't invoked from `Execute`. Not that there is anything wrong with doing that, it just isn't standard to do so. – BradleyDotNET Feb 13 '15 at 16:03
  • @dymanoid. Wouldn't implementing CanExecute() require the command to have knowledge about how it will be used? Until now I've always just had a bool CanExecute property in my view model that I bound to the IsEnabled property of the UI control. Is there an advantage to using CanExecute() instead? –  Feb 16 '15 at 22:09
  • @ConnellOD A command object *should* know if it is currently able to run. Thats what `CanExecute` is for. – BradleyDotNET Feb 17 '15 at 00:45