title: Silverlight UserControl and MVVM
body: <p>I have a very similar issue as described in <a href="https://stackoverflow.com/questions/752906/wpf-mvvm-usercontrol-with-code-behind">this post</a>.</p>
  
  <p>I have a UserControl to encapsulate an address. This contains a number of basic controls, mostly textboxes. I then have backing dependecy properties in the code-behind for each property...</p>
  
  <pre><code>#region Line1
  
      /// &lt;summary&gt; 
      /// Gets or sets the Line1.
      /// &lt;/summary&gt; 
      public string Line1
      {
          get
          {
              return (string)GetValue(Line1Property);
          }
  
          set
          {
              SetValue(Line1Property, value);
          }
      }
  
      /// &lt;summary&gt; 
      /// The Line1 dependency property.
      /// &lt;/summary&gt; 
      public static readonly DependencyProperty Line1Property =
                  DependencyProperty.Register(
                        "Line1",
                        typeof(string),
                        typeof(AddressControl),
                        new PropertyMetadata(OnLine1PropertyChanged));
  
      /// &lt;summary&gt;
      /// Line1Property property changed handler. 
      /// &lt;/summary&gt;
      /// &lt;param name="d"&gt;AddressControl that changed its Line1.&lt;/param&gt;
      /// &lt;param name="e"&gt;DependencyPropertyChangedEventArgs.&lt;/param&gt; 
      private static void OnLine1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          var control = d as AddressControl;
          if (control != null)
          {
              control.OnLine1Changed((string)e.OldValue, (string)e.NewValue);
          }
      }
  
      /// &lt;summary&gt;
      /// Called when the Line1 changes.
      /// &lt;/summary&gt;
      /// &lt;param name="oldValue"&gt;The old value.&lt;/param&gt;
      /// &lt;param name="newValue"&gt;The new value.&lt;/param&gt;
      private void OnLine1Changed(string oldValue, string newValue)
      {
          Line1TextBox.Text = newValue;
      }
  
      #endregion Line1
  </code></pre>
  
  <p>I then use this control in a view...</p>
  
  <pre><code>&lt;myControls:AddressControl Grid.Row="0" Grid.Column="3" 
                             Line1="{Binding Path=Line1, Mode=TwoWay}"/&gt;
  </code></pre>
  
  <p>This seems to set the textbox value when the view model property is updated as you would expect however my problem is getting an update from the usercontrol back to the viewmodel?</p>
  
  <p>According to the link above I should check my DataContext on the control. Surley the DataContext will be the same as the parent?</p>
  
  <p>I'm hoping an answer to this will apply to multiple nesting levels of controls ie. Control1 used in Control2 that is used in Control3, the whole point of reusability!</p>
  
  <p>Driving me nuts so any help really grateful.</p>