October 24, 2009
When working on large WPF or Silverlight applications, you can end up with a lot of XAML files. If you are using the MVVM pattern, many of those XAML files contain a View whose DataContext is expected to be set to a certain type of ViewModel object. It can sometimes become difficult to remember which Views expects which ViewModel as their DataContext. One very simple and lightweight way to help you and your team remember a View’s expected DataContext type is to leave an XML comment at the top of the XAML file. This incredibly simple and obvious technique can be a HUGE timesaver later on down the road!

I highly suggest that you get into the habit of doing this, if you haven’t done so already. You will thank yourself many times later…
15 Comments |
Praxis, mvvm |
Permalink
Posted by Josh Smith
September 30, 2009
This morning I decided to build a little WPF dialog window that would show the visual tree of some UI, and provide visual indicators over that UI when you select one of its elements. This was just a fun exercise. If you want to have a serious element tree viewer, then be sure to use Snoop instead. This quick post just shows the result of my morning fun.
Here’s the resulting dialog window in action:

Notice that a ContentPresenter is selected, which contains an image of a ninja. In the UI being analyzed, you’ll see that the ContentPresenter is decorated with a light green box:

The “Snooper” dialog window contains a TreeView that renders the visual tree of the other UI. That TreeView is declared as:

It is bound to a hierarchy of VisualElement objects. VisualElement is a class that I made to represent an element and its child elements. The important part of that class is seen below:

Notice that when a VisualElement is selected, it puts a SelectionAdorner into the adorner layer of its associated UIElement. SelectionAdorner is a class I made that just renders a rectangle around some element. It is seen below:

You can download the demo project here. NOTE: Be sure to change the file extension from .DOC to .ZIP and then decompress. WPF is fun!
4 Comments |
Debugging, Praxis |
Permalink
Posted by Josh Smith
September 7, 2009
I just published an article about a technique I use to streamline and simplify the way that ViewModels are created in an MVVM app. I’m not saying that this is the “right” way to do things, but simply something I find useful. Enjoy!
http://www.codeproject.com/KB/WPF/FromRussiaWithLove.aspx
4 Comments |
Reading Material, mvvm |
Permalink
Posted by Josh Smith
September 4, 2009
In case you ever need to prevent the user from selecting a tab in a WPF TabControl, here’s one way to do it…

The SelectedContent of the TabControl hasn’t changed at the time that the ItemsSource’s default collection view raises its CurrentChanging event. If you decide to lock the user into the selected tab, simply set the TabControl’s SelectedIndex back to the index of the SelectedContent. No fuss, no muss…
9 Comments |
Praxis |
Permalink
Posted by Josh Smith
August 23, 2009
I set aside some time today to add some goodness, and remove some badness, from my MVVM Foundation library on CodePlex. Special thanks go to Matt Heffron for his great work on making the Messenger class much safer and user-friendly. Here is a listing of the changes:
- Added safety checks and helpful error messages in Messenger for callback registration and message broadcasting. Unit tests were added to verify this behavior.
- Ensured that Messenger notifies colleagues of a message in the order they registered for that message. Unit tests were added to verify this behavior.
- Fixed an issue in an ObservableObject unit test where it failed in a Release build.
It’s exciting for me to see the improvements and ideas suggested by people using MVVM Foundation! I appreciate the time people have contributed toward making the library better. I hope there are no hard feelings when I reject a proposed feature. :)
You can get the latest source code here: http://mvvmfoundation.codeplex.com/SourceControl/ListDownloadableCommits.aspx
21 Comments |
mvvm |
Permalink
Posted by Josh Smith
July 14, 2009
Today I published a new CodePlex project, called MVVM Foundation. It is a library of my favorite classes to use in MVVM applications. Right now, it only contains classes suited for WPF applications, but eventually I will get around to adding in a Silverlight version, too. If you would like to check it out, here’s the link:
http://mvvmfoundation.codeplex.com/
12 Comments |
Announcements, mvvm |
Permalink
Posted by Josh Smith
July 11, 2009
EDIT: Shortly after publishing this blog post, I received some excellent feedback on it, and updated the PropertyObserver class to implement IWeakEventListener. Special thanks go to Huseyin Tufekcilerli for posting an IWeakEventListener implementation, which got me 90% of the way toward a better solution. This blog post and its source code download have been updated to reflect this improvement.
When working with objects that implement INotifyPropertyChanged, as most ViewModel objects tend to do, it often becomes necessary to attach a handler to their PropertyChanged event. Your code can quickly accumulate many event handling methods full of conditional statements that check if e.PropertyName equals some hard-coded string value. This leads to messy, brittle code that becomes difficult to maintain as the mess gets larger.
To combat this evil, I decided to mechanize as much of this as possible. I created a generic class called PropertyObserver<TPropertySource> which monitors the PropertyChanged event of an object, and executes whatever callback methods you have registered for when certain properties have changed. In addition, that class will verify that every property name you register with it is a real, public property on the object being observed, because the way that you provide the property name is checked by the compiler (you pass it a lambda expression that references the property). If a property name changes, but you forgot to update your code that registers the property with PropertyObserver, you will get a compiler error next time you try to build your solution.
Here are the two methods of interest:

Here is a simple example, from the source code download, that shows how to use PropertyObserver:

You can download the source code here. Note: Change the file extension from .DOC to .ZIP and then decompress it.
17 Comments |
mvvm |
Permalink
Posted by Josh Smith
May 20, 2009
Rudi Grobler recently posted an article called ‘Classic Jukebox‘ that shows how to build a jukebox application using custom hardware. It’s a very cool idea, and his article is well worth reading.
This article reminds me of a problem that I was discussing with someone at work the other day, and have been thinking about since. What are the best practices for designing an app, let’s say the Jukebox app, such that it can accomodate multiple form factors? Suppose you wanted this app to be controlled by a custom input device, or a regular mouse + keyboard, or on a multi-touch device like Microsoft Surface, or an HP TouchSmart running Windows 7.
Each form factor would bring along new, and potentially unique, interaction possibilities: Surface allows for multi-finger gestures, while a traditional keyboard allows for things like shortcut keys, etc. How should the MVVM design be used/augmented to make the input processing device-specific, without convulting the core application logic (play next song, pause, lower volume, etc)? Should there be an intermediary layer between View and ViewModel, which is perhaps an InteractionModel, to which the device-specific View is bound? This topic is something that I’m very interested in, so expect to see some more posts on it in the near future.
Food for thought…
22 Comments |
Pondering |
Permalink
Posted by Josh Smith