MVVM: Service Locator

In this post, we’ll explore the service locator (called ServiceProvider in the library) and introduce the specific services included with the MVVM Helper library (as of 1.05 anyway).  What is the Service Locator? In a nutshell, the service locator is a resolver for services that your application might need or use.  It relates a type key to a specific object implementation.  This is nothing new – the service locator design pattern has been around forever and is considered a core … [Read more...]

MVVM: Views and ViewModels

In the previous post, I provided a link to the project template you can use to start a new MVVM project using the JulMar MVVM library. Here's the two links in case you didn't get them before: MVVM Helpers Distribution Project Template Copy the project template into your Visual Studio 2008 templates directory located off your user documents - mine is at %UserProfile%DocumentsVisual Studio 2008TemplatesProjectTemplatesVisual C#Windows. Ok, so once you have these installed, what can you do with … [Read more...]

Menus and MVVM (2)

In the previous post, I wrote about using the MVVM pattern with Context Menus, but you can use it with top-level menus too -- for example, perhaps I'd like to manage a set of options through a set of checkboxes.  First, let's start with my list of options - for simplicity, we'll just make it an enumeration: public enum Test{   A,   B,   C,   D,   E,   F} Next, let's define a ViewModel to wrap this - each entry should support a … [Read more...]

Using MVVM with Menus in WPF

One question I've fielded a couple of times is how to manage menus, primarily context menus, with the MVVM pattern.  It turns out to be pretty easy once you know the "trick".  The key thing to keep in mind is that menus are just ItemsControls - they support data templating and binding like any other ItemsControl.  However, the part where people get lost is in hooking up the commands.  Here's the trick: Step 1: Define some code behind construct to manage each menu item in a … [Read more...]

More MVVM love

I recently wrote about the mini-library I tend to use, but Bill Kempf (one of the WPF disciples) released a MVVM library for WPF called Onyx which looks very appealing. I especially like his use of static reflection to implement INotifyPropertyChanged. Sweet Bill! Thanks for sharing! Check it out: WPF Onyx … [Read more...]

WPF MVVM Helper Library (WPF + MVVM = testability)

There's been a lot of talk about the Model-View-ViewModel pattern recently and it's usage around the WPF and Silverlight technology stack.  When teaching WPF, I always introduce students to MVVM as part of the Essential WPF class, it's an incredibly useful pattern that really separates the UI from the code behind behavior.  One of the things I give the students is a library to do MVVM - I also use it in my consulting work.  With all the focus on it lately, I figured maybe it's … [Read more...]

Adding to an ObservableCollection from a background thread

One annoying thing about ObservableCollection<T> is that it doesn't support modifications from background threads.  That is to say, the CollectionChanged notification doesn't marshal to the proper thread when it is raised and it causes the exception "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread".  I ran into this problem a while back and searched out to see if someone else had solved it. I … [Read more...]

(Possibly) better validations in WPF

I've never cared much for the built-in validation mechanisms provided by WPF.  I just don't think any of them feel natural to the way we build WPF applications today.  Basically, there are essentially three mechanisms built into WPF for validations: Validation Rules Exceptions IDataErrorInfo Validation rules are checked prior to transferring the value from the bound control to your source (typically the business object).  Exceptions are a form of validation rule - if the property … [Read more...]