title: WPF Custom Control: DependencyProperty never Set (on only 1 of many properties)
body: <p>I have made a custom control called <strong>AddressForm</strong>, which inherits from Control. The control is used to display the fields for an IAddress object. </p>
  
  <p>Originally I made this control in Silverlight, now I am trying to get it working in WPF .net 4.5</p>
  
  <p>The control defines 9 different dependency properties, and all but one is working correctly. Naturally, the one that is not working is the Address object itself!</p>
  
  <p><strong>The Control's Address property never receives a value.</strong> I put a break-point in the address's Getter, the property is getting called, the address object is not null, but the control doesn't receive it.</p>
  
  <p>There are no exceptions, nor error messages in the output screen.</p>
  
  <p><strong>Control :</strong></p>
  
  <pre><code>public class AddressForm : Control, INotifyPropertyChanged
  {
      [...]
  
      public static readonly DependencyProperty AddressProperty = DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new PropertyMetadata( AddressChanged));
      public IAddress Address 
      {
          get { return (IAddress)GetValue(AddressProperty); }
          set { SetValue(AddressProperty, value); } 
      }
  
      private static void AddressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          //break-point here never gets hit
          AddressForm form = d as AddressForm;
          if (form != null)
              form.OnAddressSet();
      }
  
      private void OnAddressSet()
      {
          //break-point here never gets hit
          if (StateProvince != null &amp;&amp; Address != null)
          SelectedStateProvince = StateProvince.Where(A =&gt; A.StateProvince == Address.StateProvince).FirstOrDefault();
      }
  
      [...]
  }
  </code></pre>
  
  <p>(Other DependencyProperties are set in the same way and work correctly.)</p>
  
  <p><strong>xaml :</strong></p>
  
  <pre><code>&lt;Address:AddressForm Address="{Binding SelectedMFG.dms_Address, Mode=TwoWay}" ... /&gt;
  </code></pre>
  
  <p>the type of SelectedMFG is scm_MFG </p>
  
  <p><strong>Data objects:</strong></p>
  
  <pre><code>public partial class scm_MFG 
  {
      [...]
      public virtual dms_Address dms_Address { get; set; } //break-point here never enables? Generated code from Entity TT
  
      //Another attempt, trying to determine if the IAddress cast was the cause of the issue
      //Address="{Binding SelectedMFG.OtherAddress}" 
      public IAddress OtherAddress 
      {
          get { 
              return dms_Address as IAddress; //break-point here gets hit. dms_Address is not null. Control never receives the value.
          } 
      }
  }
  
  public partial class dms_Address : IAddress, INotifyPropertyChanged { ... }
  </code></pre>
  
  <p><strong>My Attempts:</strong></p>
  
  <p>I have tried accessing the dms_Address property different ways. I can display the values of the Address in a textbox, which tells me there is no problem with the datacontext.</p>
  
  <pre><code>&lt;TextBox  Text="{Binding SelectedMFG.dms_Address.StreetName, Mode=TwoWay}" /&gt; &lt;!-- this value displays --&gt;
  </code></pre>
  
  <p>I have also tried changing the dependency property's registration metadata. 
  I'm not sure which is the correct one to use: <strong>PropertyMetadata, UIPropertyMetadata</strong> or <strong>FrameworkPropertyMetadata</strong> </p>
  
  <pre><code>DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new PropertyMetadata(AddressChanged));
  DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new PropertyMetadata(null, AddressChanged));
  DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new UIPropertyMetadata(AddressChanged));
  DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, AddressChanged));
  // and other FrameworkPropertyMetadataOptions, no difference
  </code></pre>
  
  <p>.</p>
  
  <p>I <strong>believe</strong> I have done everything correctly and that this <strong>should</strong> be working. </p>
  
  <p>Does anything jump out as odd or incorrect?</p>