Create a new ICommand object in the ViewModel

advertisements

Both ICommand objects are bound to a ViewModel.

The first approach seems to be used often.

But the second one saves some lines of code but would it not create everytime a new ICommand object when the Binding is refreshed so its a waste of resources?!

private LightCommand _deleteDocumentCommand;
        public LightCommand DeleteDocumentCommand
        {
            get { return _deleteDocumentCommand ?? (_deleteDocumentCommand = new LightCommand(() => DeleteDocument(), () => CanDeleteDocument)); }
        }

        public LightCommand DeleteDocumentCommand
        {
            get { return new LightCommand(() => DeleteDocument(), () => CanDeleteDocument); }
        }


yes it would. You are better off instantiating it once, so that you have something like this:

LightCommand DeleteCommand { get;  set;}

and then in our VM instantiation you assign to it. Or you could use your first example.