title: Dependency Property reset after setting DataContext
body: <p>I have a UserControl (AgreementDetails) in WPF with the following DependencyProperty and function:</p>
  
  <pre><code>// UserControl AgreementDetails
      public int AgreementID
          {
              get { return Convert.ToInt32(GetValue(AgreementIDProperty)); }
              set { SetValue(AgreementIDProperty, value); }
  
          }
  
      public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
  
      private void UserControl_Loaded(object sender, RoutedEventArgs e)
      {
          int id = AgreementID;
  
          if (id &gt; 0)
          {
              GetData();
              SetBindingContext();
              this.Visibility = System.Windows.Visibility.Visible;
          }
          else
          {
              this.Visibility = System.Windows.Visibility.Collapsed;
          }
      }
  
      private void GetData()
      {
          ConsultantServiceClient client = new ConsultantServiceClient();
          _contract = new UC1001_ActiveAgreementContract();
          _contract = client.GetAgreementDetailsByAgreementID(AgreementID);
      }
  
      private void SetBindingContext()
      {
          this.DataContext = _contract;
      }
  </code></pre>
  
  <p>I use this UserControl to show as a tooltip in another UserControl (Dashboard) where I set the AgreementID property:</p>
  
  <pre><code>// Dashboard    
  &lt;Setter Property="DataGridCell.ToolTip"&gt;
        &lt;Setter.Value&gt;
               &lt;my:UC1001_AgreementDetails_View Background="#FFF" Opacity="0.88" AgreementID="{Binding Months[9].AgreementID}"/&gt;
        &lt;/Setter.Value&gt;
  &lt;/Setter&gt;
  </code></pre>
  
  <p>In AgreementDetails, I use the AgreementID to get some data from the database to show in the UserControl. The first time I do this, everything goes smooth. But when I set the incoming WCF DataContract as the datacontext in AgreementDetails, the AgreementID property resets to 0, so the second call will not work because obviously I do not have an agreement with AgreementID = 0. I checked and the AgreementID resets in the SetBindingContext(); method after the DataContext is set.</p>
  
  <p>How can I make it so the AgreementID property will not reset after I set a new dataContext in AgreementDetails??</p>
  
  <p>More information can be provided if wanted.</p>
  
  <p><strong>EDIT</strong>: I now have the following code:</p>
  
  <pre><code>// Dependency properties
   public int AgreementID
   {
      get { return (int)GetValue(AgreementIDProperty); }
      set { SetValue(AgreementIDProperty, value); }
   }
  
   public UC1001_ActiveAgreementContract AgreementDetailsContract
   {
      get { return (UC1001_ActiveAgreementContract)GetValue(AgreementDetailsContractProperty); }
      set { SetValue(AgreementDetailsContractProperty, value); }
  
   }
  
      public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
      public static readonly DependencyProperty AgreementDetailsContractProperty = DependencyProperty.Register("AgreementDetailsContract", typeof(UC1001_ActiveAgreementContract), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
  
      private void UserControl_Loaded(object sender, RoutedEventArgs e)
      {
          int id = AgreementID;
  
          if (id &gt; 0)
          {
              GetData();
              SetBindingContext();
              this.Visibility = System.Windows.Visibility.Visible;
          }
          else
          {
              this.Visibility = System.Windows.Visibility.Collapsed;
          }
      }
  
      private void GetData()
      {
          ConsultantServiceClient client = new ConsultantServiceClient();
          AgreementDetailsContract = client.GetAgreementDetailsByAgreementID(AgreementID);
      }
  
      private void SetBindingContext()
      {
         this.DataContext = AgreementDetailsContract;
      }
  </code></pre>
  
  <p>I still have the the problem that the AgreementID resets to 0 after the DataContext is set.</p>
  
  <p>Also when I use the following statement to bind, I get an empty label:</p>
  
  <pre><code>&lt;Label Content="{Binding RelativeSource={RelativeSource Self}, Path=AgreementDetailsContract.EndClientName}" /&gt;
  </code></pre>
  
  <p><strong>SOLVED:</strong></p>
  
  <p>I removed the SetDataBinding() method so the Binding doesn't reset my DependencyProperty, and for the Binding of my labels I used the following Binding (instead of RelativeSource Self):</p>
  
  <pre><code> &lt;Label Content="{Binding ElementName=AgreementDetails, Path=AgreementDetailsContract.EndClientName}" Grid.Column="1" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="11,0,0,0" Name="_labelEindklant" VerticalAlignment="Top" /&gt;
  </code></pre>
  
  <p>ElementName=AgreementDetails is the name of my UserControl. Strange enough with {RelativeSource Self} it didn't work...</p>