tags:

views:

23

answers:

1

Commands here would be some variation of Josh Smith's RelayCommand (I call mine a VmCommand), and the question is abouthow your ViewModel creates them. I'm aware of two basic techniques, the first being that you set up all command properties inside the constructor, the second being you instantiate the command lazily inside a property getter.

I prefer the latter as I feel it lets me keep my code more organized, as I will typically wrap all behavior related to a given feature in it's own region, as shown below for a SaveCommand.

How do you like to setup your commands?

Cheers,
Berryl

    #region Saving

    public ICommand SaveCommand
    {
        get
        {
            return _saveCommand ?? (_saveCommand = new VmCommand
            {
                CanExecuteDelegate = x => CanSave(),
                ExecuteDelegate = x => Save()
            });
        }
    }
    private ICommand _saveCommand;

    private bool CanSave() { return IsDirty; }

    public void Save()
    {
        _facade.Save();
    }

    #endregion
+1  A: 

That's about what I do... however, when IsDirty changes, don't expect your buttons to automatically enable themselves.

You'll need to tell your command to fire CanExecuteChanged and call CommandManager.InvalidateRequerySuggested, otherwise you'll find your buttons don't always respond to changes to the "can execute" status of the command.

Will