So you’ve decided that Windows Forms is more trouble than its worth. You’re all set to start writing a WPF app. You fire up Visual Studio 2008, create a new solution, and see absolutely nothing about this fancy MVVM that seems to have taken the world by storm. No standard templateâ€”just a “WPF Application” that, when you start it, gives you the same blank Window that you used to get when you were creating Windows Forms apps. Really?
To be fair, I don’t think even the gang in Redmond realized just how far the community would run with MVVM and a lot of what we think of as staples of MVVM design now simply weren’t even thought of when Microsoft was trying to rush VS 2008 out the door. Visual Studio 2010 will provide better out-of-the-box guidance in creating a well-factored WPF app, but that’s not here quite yet.
Until then, give Order Entry a whirl. It’s a simple app with a simple purposeâ€”provide a simple order entry ticket (buy/sell, symbol, price, and quantity). It sets up the basics of defining a view and a viewmodel that exposes the properties of a model in a way more conducive to consumption by the view.
- Binding a view to a viewmodel
- Separation of the view (visual elements) from the viewmodel (data, non-visual resources)
- A simple ICommand implementation that disables/enables the Submit button
It’s not perfect because it lacks two things that every real GUI has (which we’ll fix in later posts):
- Threading. When you press Submit, all the “work” happens on the GUI thread.
- Validation. When you type something in the view that is not physically storable in the viewmodel (like entering in “Abracadabra” as a quantity), the screen changes to indicate an error on the field, but it doesn’t stop the user from submitting (viewmodel and its ICommand don’t know that the view is in an error stateâ€”only the view knows).
Validation and WPF data binding are tricky to get right. We’ll talk about that in later posts too. But for now, here’s version 1 of the Order Entry app to chew on. â€”DKT