Commanding in Prism


In the screenshot below, the button is enabled because the text box does not contain ‘NO’:


In the screenshot below, the button is disabled because the text box does contain ‘NO’:


The comments in the code screenshots probably cover everything:




using System.Windows;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System.Windows.Input;

namespace CommandImpl.ViewModel {

/// A basic test of the Prism implementations of Commanding, using DelegateCommand and NotificationObject.
/// Next steps are how to test this.

public class CommandViewModel : NotificationObject {

private ICommand _submitCommand;
public ICommand SubmitCommand {
get { return _submitCommand; }
set { if (_submitCommand == value) return;
_submitCommand = value;
RaisePropertyChanged(() => SubmitCommand);

public CommandViewModel() {
SubmitCommand = new DelegateCommand(Submit, CanSubmit);

public bool CanSubmit(string cmdParameter) {
if (cmdParameter == null) return true;
return !(cmdParameter.Contains(“NO”));

private static void Submit(string cmdParameter) {
// Although I only want the command parameter for the CanSubmit, it appears there is no ctor
// that takes no parameter for the Execute, but a parameter for the CanExecute
// The idea is possibly I guess that you pass-through the value to the CanExecute method.
// And the plus anyway is that if CanSubmit returns false, you never reach here anyway.
// But given that, it does mean that cmdParameter is always redundant, as you clearly do not invoke
// CanSubmit from here.
var msg = string.Format(“Value in textbox: [{0}]”, cmdParameter);
//msg = CanSubmit(cmdParameter) ? “Can execute” : “Cannot execute”;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s