title: MVVM when to create a viewmodel for a control?
body: <p>Or should I only create viewmodels for the domain data being represented? While reading on MVVM, I came across this:</p>
  
  <p>"The ViewModel is responsible for these tasks.  The term means "Model of a View", and can be thought of as abstraction of the view, but it also provides a specialization of the Model that the View can use for data-binding.  In this latter role the ViewModel contains data-transformers that convert Model types into View types, and it contains Commands the View can use to interact with the Model.  "</p>
  
  <p><a href="http://blogs.msdn.com/b/johngossman/archive/2005/10/08/478683.aspx" rel="nofollow">http://blogs.msdn.com/b/johngossman/archive/2005/10/08/478683.aspx</a></p>
  
  <p>If the viewmodel is a model of the view, then doesn't it make sense to put properties of the view in the viewmodel rather than on the code behind of the view itself?</p>
  
  <p>I guess in making a custom control I just have a hard time deciding when I should just add a property to the control's code behind and when it is worthwhile to make a viewmodel for the control to represent it.  Honestly I kind of feel that moving all of the control's view related properties to the viewmodel would clean up the code behind of the control leaving only the control logic.</p>
  
  <p>However, if I were to change things like this, then at times when an item needs properties from the control itself I can no longer use {Binding ElementName = control, Path=property} and have to instead get the data context of the parent (because the current datacontext would be on the individual subitem of the observable collection.</p>
  
  <p>Basically I was considering whether I should move properties from Class GraphViewer into a GraphViewerViewModel and then just bind to it.</p>
  
  <p>Code is worth a million words so:
      public class GraphViewerViewModel :DependencyObject
        {
          private const int DEFAULT_PEN_WIDTH = 2;
          private const int DEFAULT_GRAPH_HEIGHT = 25;</p>
  
  <pre><code>    public SignalDataViewModel _SignalDataViewModel
      {
        get;
        set;
      }
      public PreferencesViewModel _PreferencesViewModel
      {
        get;
        set;
      }
  }
  </code></pre>
  
  <p>Meanwhile </p>
  
  <pre><code>public class SignalDataViewModel : INotifyPropertyChanged
    {
      public event PropertyChangedEventHandler PropertyChanged;
  
      ObservableCollection&lt;SignalViewModel&gt; _signals;
      public ObservableCollection&lt;SignalViewModel&gt; Signals
      {
        get
        {
          return _signals;
        }
        private set
        {
          _signals = value;
        }
      }
  
      ObservableCollection&lt;SignalViewModel&gt; _AddedSignals;
      public ObservableCollection&lt;SignalViewModel&gt; AddedSignals
      {
        get
        {
          return _AddedSignals;
        }
        private set
        {
          _AddedSignals = value;
        }
      }
  </code></pre>
  
  <p>it is a pain to type:</p>
  
  <pre><code>PenWidth="{Binding RelativeSource = {RelativeSource AncestorType={x:Type DaedalusGraphViewer:GraphViewer}}, 
                                    Path = _GraphViewerViewModel._SignalDataViewModel._AxisDivisionUnit.GraphPenWidth, Mode=OneWay}"
  </code></pre>
  
  <p>and I'm wondering if it is worthwhile to make the change or whether I'm misunderstanding what a view model should be used for in mvvm.</p>