Commanding in Prism

commanding01

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

commanding02

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

commanding03

The comments in the code screenshots probably cover everything:

commanding04

commanding05

commanding06


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.
/// http://msdn.microsoft.com/en-us/library/gg431410(v=pandp.50).aspx
// http://msdn.microsoft.com/en-us/library/system.windows.input.icommand(v=vs.110).aspx
// http://jeremybytes.blogspot.co.uk/2012/12/five-months-in-working-with-prism-in-wpf.html
// http://msdn.microsoft.com/en-au/library/ff921102.aspx
// http://msdn.microsoft.com/en-us/library/gg419054(v=pandp.50).aspx
/// 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);
MessageBox.Show(msg);
//msg = CanSubmit(cmdParameter) ? “Can execute” : “Cannot execute”;
//MessageBox.Show(msg);
}
}
}

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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