title: Pragmatic use of code-behind in MVVM pattern
body: <p>I'm trying to follow the MVVM pattern in a WPF application as good as I can, mainly to be able to create unit tests for my ViewModel logic.</p>
  
  <p>In most cases data binding between ViewModel properties and properties of visual elements works fine and is easy. But sometimes I encounter situations where I cannot see an obvious and straightforward way while a solution to access and manipulate controls from code-behind is very easy.</p>
  
  <p>Here is an example of what I mean: <strong>Inserting a text fragment into a <code>TextBox</code> at the current caret position</strong></p>
  
  <p>Since <code>CaretIndex</code> isn't a dependency property it can't be bound directly to a ViewModel's property. <a href="https://stackoverflow.com/questions/980219/handling-cursor-inside-textbox/980317#980317">Here</a> is a solution to work around this limitation by creating a dependency property. And <a href="https://stackoverflow.com/questions/2117259/insert-text-into-wpf-textbox-at-caret-position/2117315#2117315">here</a> is the solution to do this in code-behind. I would prefer the code-behind way in this situation. Another problem I recently had was binding a dynamic collection of columns to a WPF datagrid. It was clear and simple to program in code-behind. But for a MVVM-friendly databinding approach I could only find work arounds in several blogs which all looked quite complex to me and had various limitations in one or the other aspect.</p>
  
  <p>I don't want to keep the MVVM architecture clean of code-behind logic at all costs. If the amount of work arounds is too big, a MVVM-friendly solution requires a lot of code which I don't fully understand (I'm still a WPF beginner) and is too time consuming I prefer a code-behind solution and sacrifice automatic testability of a few parts of my application.</p>
  
  <p>For the mentioned pragmatic reasons I am looking now for "patterns" to make controlled use of code-behind in an application without breaking the MVVM architecture or without breaking it too much.</p>
  
  <p>Up to now I've found and tested two solutions. I will draw rough sketches with the Caret Position example:</p>
  
  <p><strong>Solution 1) Give the ViewModel a reference to the View through an abstract interface</strong></p>
  
  <ul>
  <li><p>I would have an interface with methods which would be implemented by the view:</p>
  
  <pre><code>public interface IView
  {
      void InsertTextAtCaretPosition(string text);
  }
  
  public partial class View : UserControl, IView
  {
      public View()
      {
          InitializeComponent();
      }
  
      // Interface implementation
      public void InsertTextAtCaretPosition(string text)
      {
          MyTextBox.Text = MyTextBox.Text.Insert(MyTextBox.CaretIndex, text);
      }
  }
  </code></pre></li>
  <li><p>Inject this interface into the ViewModel</p>
  
  <pre><code>public class ViewModel : ViewModelBase
  {
      private readonly IView _view;
  
      public ViewModel(IView view)
      {
          _view = view;
      }
  }
  </code></pre></li>
  <li><p>Execute code-behind from a ViewModel's command handler through the interface methods</p>
  
  <pre><code>public ICommand InsertCommand { get; private set; }
  // Bound for instance to a button command
  
  // Command handler
  private void InsertText(string text)
  {
      _view.InsertTextAtCaretPosition(text);
  }
  </code></pre></li>
  </ul>
  
  <p>To create a View-ViewModel pair I would use dependency injection to instantiate the concrete View and inject it into the ViewModel.</p>
  
  <p><strong>Solution 2) Execute code-behind methods through events</strong></p>
  
  <ul>
  <li><p>The ViewModel is publisher of special events and command handlers raise those events</p>
  
  <pre><code>public class ViewModel : ViewModelBase
  {
      public ViewModel()
      {
      }
  
      public event InsertTextEventHandler InsertTextEvent;
  
      // Command handler
      private void InsertText(string text)
      {
          InsertTextEventHandler handler = InsertTextEvent;
          if (handler != null)
              handler(this, new InsertTextEventArgs(text));
      }
  }
  </code></pre></li>
  <li><p>The View subscribes to these events</p>
  
  <pre><code>public partial class View : UserControl
  {
      public View()
      {
          InitializeComponent();
      }
  
      private void UserControl_Loaded(object sender, RoutedEventArgs e)
      {
          ViewModel viewModel = DataContext as ViewModel;
          if (viewModel != null)
              viewModel.InsertTextEvent += OnInsertTextEvent;
      }
  
      private void UserControl_Unloaded(object sender, RoutedEventArgs e)
      {
          ViewModel viewModel = DataContext as ViewModel;
          if (viewModel != null)
              viewModel.InsertTextEvent -= OnInsertTextEvent;
      }
  
      private void OnInsertTextEvent(object sender, InsertTextEventArgs e)
      {
          MyTextBox.Text = MyTextBox.Text.Insert(MyTextBox.CaretIndex, e.Text);
      }
  }
  </code></pre></li>
  </ul>
  
  <p>I am not sure if the <code>Loaded</code> and <code>Unloaded</code> events of the <code>UserControl</code> are good places to subscribe and unsubscribe to the events but I couldn't find problems during test.</p>
  
  <p>I have tested both approaches in two simple examples and they both seem to work. Now my questions are:</p>
  
  <p><strong></p>
  
  <ol>
  <li><p>Which approach do you think is preferable? Are there any benefits or downsides of one of the solutions which I possibly don't see?</p></li>
  <li><p>Do you see (and perhaps practice) other solutions?</p></li>
  </ol>
  
  <p></strong></p>
  
  <p>Thank you for feedback in advance!</p>