WPF Commands

|
Commands in WPF rock, they provide a very nice abstraction from the implementation and invocation of what you would consider a command in an application. I have been learning about WPF over the last few weeks and I have found many subjects in that technology that I really like; but commands were not something that I groked immediately.

The reason being I read about them first in Chris Andersons book Essential Windows Presentation Foundation (an excellent book), at the time I read it I liked the idea and moved on – until I read about them in Chris Sells and Ian Griffiths book Programming Windows Presentation Foundation, here I was introduced to the fact that WPF provides a whole bunch of commands. This was the point I missed in Chris A’s book, as he presents the topic by getting you to build a command, slowly taking you one step at a time – but he never does the big reveal, he seemed to save that for Chris S!

What I’m going to do here is show the simple XAML required to use a command in a couple places, like a menu and a button and then how to wire those up to a piece of code. For all the gory details on commands in WPF I’ll refer you to Chris and Chris. However, what I think is lacking is a simple example of how these ideas come together in a simple example (more...)

4 comments:

Alan Le said...

I agree. Commands in WPF rocks!

Btw, Chris Sells' first edition book is outdated. You no longer need to import the Input namespace in XAML. The syntax you wrote still works, however it can be simplified as follows:

< MenuItem Header="E_xit" Command="ApplicationCommands.Close" />

and

< CommandBinding Command="ApplicationCommands.Close" Executed="OnCloseWindow" />

Paul said...

Thanks for you feedback Alan. I have updated the article to include a note that states that the namespaces are not required and offers the same example you gave me.

You're right in that the Chris Sells book is out of date on this (it was written against Beta 2 of WPF and uses a different method of namespace aliasing to the one described in this article); this article was pulled together also using Chris Anderson's book which is the one that contains the technique described in the article. I'm sure that namespace aliasing is still required for your own types and for some .Net Framework elements too.

What I'm driving at here is the mistake was mine not Chris A's. I pulled the sample together based on what he taught me :-) However, I did not try it without the namespace, I just assumed I'd need it, which is my bad.

Thanks again for your feedback, I learnt something new, which is what the post was all about.

-PJ

Ian Griffiths said...

I don't recall ever saying you needed to import the input namespace into XAML in the 1st edition. I just checked it now and can't find anything that says that, so I've no idea what your original posted was on about.

I wrote the chapter that dealt with input by the way, not Chris.

In the 2nd edition (due out really soon now) Input gets its own chapter, and we have more details on commands.

Paul said...

Thanks for replying Ian, apologies if there has been a misunderstanding here - in my reply to Alan I stated that the mistake in the importing of the namespace was mine, neither your work or Chris A's said to do it.

However, the work I'd been doing with Chris A's book had put me in that "mind set"; which is probably why I did it:

this article was pulled together also using Chris Anderson's book which is the one that contains the technique described in the article

As for the 2nd edition of your book, I will definitely be in queue to purchase it - I have certainly found it extremely useful and would welcome an up-to-date version; excellent work.

Thanks also for providing the opportunity to clear up any misunderstandings.

-PJ