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

The Prism Developer’s Guide

Ever seen this?

PrismManual01

The manual is good and it’s free, and the full download comes with a ton of worked examples. This is the Prism 4 Guide, and this is the Prism 4.1 download, including the examples.

In fact here’s one I hadn’t noticed – a manual about Prism and Windows 8.1 – surely worth a browse.

PrismManual02

PrismManual03

And just searching for DelegateCommand references, just as a for instance…

PrismManual04

Slightly tangentially, you can replace the RelayCommand references with Prism DelegateCommands, and everything continues to work:

ICommand05

ICommand06

ICommand01

ICommand02

ICommand07

ICommand03

ICommand04

The top row of Order 11088 is one I created using the Add button, edited to change the Freight value, and then used the Save button.

To prove it made its way to the database:

ICommand08

May 2015…

And now we have this

PrismLatest01

, and this

PrismLatest02

The date suggests I just missed it last time round. This looks good:

PrismLatest03

And I should back this up somewhere in case it disappears:

PrismLatest04

Argh… looks to be the same. But for (my) reference I’ve saved it to my Adobe cloud.

And a final link to the QuickStarts etc.

Diary of a reluctant gardener

So no direct evidence of me doing stuff, so you’ll just have to take my word for for it:

GardeningWork01

The work is replacing… anyway, it’s hard, it’s not my favourite thing, but once it’s all over, it will look nice.

I am aware that I have done zero music for some time now. But I haven’t been idle. I have my plan.

Anyway… Prism: Prism libraries implement the INotifyPropertyChanged etc and ICommand interfaces. For now, this is my cheatsheet for ICommand (right now, just an example of a by-hand implementation):

Office Lens_20140801_231330_processed (1)