There is much discussion on the web about the Model-View-ViewModel pattern - the defacto design pattern for developing WPF and Silverlight applications. In the MVVM pattern the WPF / Silverlight databinding mechanism is used to push/pull data to and from the ViewModel. The View doesn't have knowledge of the Type of the ViewModel, and the ViewModel has no reference to the View. This results in a neat decoupled architecture where ViewModel and Model may be Unit Tested as it is cleanly separated from the View.

MVVM is a great pattern and should be adhered to in 99% of circumstances, however; it does have some potential shortfalls from time to time. What about a circumstance where C# code is needed to directly manipulate the view, perhaps because of a limitation of XAML binding or because of a control suite you are using that has limited DataBinding support? In these cases it is common to pollute the code-behind file by accessing data from the ViewModel via datacontext. If you've ever seen code like this in your codebase you'll know what I'm talking about.

// Inside MyViewModel.cs
public class MyViewModel : INotifyPropertyChanged
{
    // ...
}

// ... 
// Inside MyControl.xaml.cs
public class MyControl : UserControl
{
    public MyControl() 
    {
        InitializeComponent(); 
    }

    pubilc void MyControl_Loaded(object sender, EventArgs e)
    {
         var myViewModel = DataContext as MyViewModel;
         if (myViewModel != null) 
         {
              // Get some data off myviewmodel  
              // and manipulate the view
              // ... 
         }
    }
}

Now admittedly many times where you see this, it is because the developer has an inadequate knowledge of MVVM or databinding. For instance, many tasks which at first glance would require this method can be achieved through Xaml. Using Expression Blend's Interactivity Behaviors to transform a mouse click into an ICommand.Execute call inside your viewmodel gets around the problem of how to forward a mouse click event from your code-behind to the ViewModel. However what if this still isn't enough, and you need direct access to the View from the ViewModel (or vice versa?).
The only way is to break the pattern, unless, we can inject a View directly into our ViewModel. Of course, we would have to inject it by interface so as to maintain testability. This would result in a hybrid pattern, somewhere between Model-View-Presenter and Model-View-View-Model.

I call it MiVVM, or Model-interface-to-View-ViewModel

In vanilla MVVM, the View and ViewModel have no knowledge of each other and all communication is handled by data binding (Figure 1). In MiVVM or Model Interface-to-View View Model, we propose that two modifications are made to extend the pattern. Firstly, an interface is added to the View, in this case, IView (Figure 2). Secondly, the View must be injected into the ViewModel without polluting the code-behind of the View and in keeping with a general principle of MVVM to minimise or even eliminate code behind.

 


Figure 1: Vanilla MVVM UML Diagram

Figure 2: MiVVM UML Diagram, showing interface to View and relationship to ViewModel

To achieve the injection of a View, we can use Data Binding. Xaml allows for OneWayToSource bindings which fire from View to ViewModel on property changed and also custom attached property classes can be developed to assist this action. Two examples are shown below in Figure 3 and Figure 4.

 
 


/// 
/// Defines an interface to the usercontrol to 
/// manipulate directly from ViewModel
/// 
public interface IMyControl
{
    // Our test method to call 
    void CallView(string message);
}

/// 
/// Defines the usercontrol 
/// 
public partial class MyControl : UserControl, IMyControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    public void CallView(string message)
    {
        MessageBox.Show(message);
    }
}
public class MyViewModel
{
    private IMyControl myControl;

    public IMyControl InjectedUserControl
    {
        set
        {
            Debug.WriteLine(string.Format("Received Injected Control \"{0}\"", value == null ? "NULL" : value.GetType().Name));

            this.myControl = value;
            this.OnInjectedObjectsChanged();
        }
    }

    private void OnInjectedObjectsChanged()
    {
        // Directly access the view via its interface
        if (this.myControl != null)
            this.myControl.CallView("Hello From MyViewModel");
    }
}

Download the demo project to see two different implementations of MiVVM. the first uses a Dependency property on the view itself, however in the case that this isn't possible the second method uses an attached property to inject the View directly into the ViewModel. In our opinion the second implementation is better as this is cleaner from a code-behind perspective, and moves the logic where it should be to the Xaml