title: Connect ViewModel to Model
body: <p>I'm working on an application and spent quite some time finding a solution for the following problem. I've tried several things but I'm not sure what's the best approach to solve it.</p>
  
  <p>I've got a setup of a View, ViewModel and a Model. The ViewModel contains a collection of models of which the user can select a specific model (the collection can be empty which makes the selected model null).</p>
  
  <p>The View consists of a Combobox which allows the user to select a model out of a collection of models. The selected model will then be displayed in the view (with various controls such as treeviews, labels, ...).</p>
  
  <p>This is the Model (I will keep it simple for the sake of explanation):</p>
  
  <pre><code>public Model {
      private int id;
      private String name;
  
      public Model(_id, _name) {
          id = _id;
          name = _name;
      }
  
      public int ID {
          get { return id; }
          set { id = value; }
      }
      public String Name {
          get { return name; }
          set { name = value; }
      }
  }
  </code></pre>
  
  <p>And the ViewModel:</p>
  
  <pre><code>public ViewModel : INotifyPropertyChanged {
      private ObservableCollection&lt;Model&gt; models = new ObservableCollection&lt;Model&gt;();
      private Model selectedModel;
  
      public ViewModel() { }
  
      public Model SelectedModel {
          get { return selectedModel; }
          set { 
              selectedModel = value; 
              NotifyChanged("SelectedModel");   
          }
      }
      public Model ModelCellection {
          get { return models; }
          set { 
              models = value; 
              NotifyChanged("ModelCellection");   
          }
      }
  
      public void addModel(Model newModel) {
          models.Add(newModel);
      }
  
      //variant A
      public int ID {
          get { return (selectedModel == null) ? 0 : selectedModel.ID; }
          set {
              if(selectedModel == null)
                  return; 
              selectedModel.ID = value; 
              NotifyChanged("ID");   
          }
      }
      public String Name {
          get { return (selectedModel == null) ? 0 : selectedModel.Name; }
          set {
              if(selectedModel == null)
                  return; 
              selectedModel.Name = value; 
              NotifyChanged("Name");   
          }
      }
  
      //variant B (the attributes of the model will be copied when the selected model changes)
      public void changeSelectedModel(Model newSelectedModel) {
          Name = newSelectedModel.Name;
          Id = newSelectedModel.Id;
      }
      private int id = 0;
      private String name = String.Empty;
      public int ID {
          get { return id; }
          set {
              id = value; 
              NotifyChanged("ID");   
          }
      }
      public String Name {
          get { return name; }
          set {
              name = value; 
              NotifyChanged("Name");   
          }
      }
  }
  </code></pre>
  
  <p>My Question is pretty simple: How do I access the relevant public properties of the Model via the ViewModel?</p>
  
  <p>The solutions I've tried so far are:</p>
  
  <ul>
  <li>Variant A: Just change the selected model and use proxy properties to access the properties of the model (problem: The view doesn't get updated when the selected model changes because the Name and Id properties of the ViewModel don't raise a PropertyChanged event)</li>
  <li>Variant B: Copy the relevant properties of the model when the selected model is changed (problem: I have to copy the properties of the model)</li>
  </ul>
  
  <p>After talking to some guys in the wpf chat I switched from variant B to variant A but I don't know how to solve the problem of updating the view when the selected model changes? Or would you rather suggest to use a completely different approach to solve this?</p>