title: Hair loss and MVVM user controls
body: <p>I have a user control written in C# &amp; WPF using the MVVM pattern.</p>
  
  <p>All I want to do is have a property in the bound ViewModel exposed to outside of the control. I want to be able to bind to it and I want any changes to the property to be picked up by anything outside the control that is bound to the exposed value.</p>
  
  <p>This sounds simple, but its making me pull out my hair (and there is not much of that left). </p>
  
  <p>I have a dependency property in the user control. The ViewModel has the property implementing the INotifyPropertyChanged interface and is calling the PropertyChanged event correctly.</p>
  
  <p>Some questions:
  1) How do I pick up the changes to the ViewModel Property and tie it to the Dependency Property without breaking the MVVM separation? So far the only way I've managed to do this is to assign the ViewModels PropertyChanged Event in the Controls code behind, which is definitely not MVVM.</p>
  
  <p>2) Using the above fudge, I can get the Dependency property to kick off its PropertyChangedCallback, but anything bound to it outside the control does not pick up the change. </p>
  
  <p>There has to be a simple way to do all of this. Note that I've not posted any code here - I'm hoping not to influence the answers with my existing code. Also, you'd probably all laugh at it anyway...</p>
  
  <p>Rob</p>
  
  <p>OK, to clarify - code examples:</p>
  
  <p>usercontrol code behind:</p>
  
  <pre><code>   public static DependencyProperty NewRepositoryRunProperty = DependencyProperty.Register("NewRepositoryRun", typeof(int?), typeof(GroupTree),
                                                                  new FrameworkPropertyMetadata( null, new PropertyChangedCallback(OnNewRepositoryRunChanged)));
      public int? NewRepositoryRun
      {
          get { return (int?)GetValue(NewRepositoryRunProperty); }
          set
          {
              SetValue(NewRepositoryRunProperty, value);
          }
      }
  
      private static void OnNewRepositoryRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          if (e.OldValue != e.NewValue)
          {
  
          }
      }
  
      public GroupTree()
      {
          InitializeComponent();
  
          GroupTreeVM vm = new GroupTreeVM();
  
          this.DataContext = vm;
  
      }
  </code></pre>
  
  <p>Viewmodel (GroupTreeVM.cs)</p>
  
  <pre><code>   private int? _NewRepositoryRun;
      public int? NewRepositoryRun
      {
          get
          {
              return _NewRepositoryRun;
          }
          set
          {
              _NewRepositoryRun = value; 
              NotifyPropertyChanged();
          }
      }
  </code></pre>