title: Constraint to limit the scope of an attached dependency property
body: <p>Is there a way to add a constraint to an attached dependency property so that it only can be applied to a specific type, something in the metadata?</p>
  
  <p>If not, is it meaningful to explicit type the static Get-and Set-methods of the attached DPs?</p>
  
  <p><strong>Example:</strong></p>
  
  <p>If I have for example the following declaration:</p>
  
  <pre><code>public static int GetAttachedInt(DependencyObject obj) {
      return (int)obj.GetValue(AttachedIntProperty);
  }
  
  public static void SetAttachedInt(DependencyObject obj, int value) {
      obj.SetValue(AttachedIntProperty, value);
  }
  
  public static readonly DependencyProperty AttachedIntProperty = 
     DependencyProperty.RegisterAttached("AttachedInt", typeof(int), 
     typeof(Ownerclass), new UIPropertyMetadata(0));
  </code></pre>
  
  <p>would it be meaningfull to change it as follows, to only apply it to TextBoxes?</p>
  
  <pre><code>public static int GetAttachedInt(TextBox textBox) {
      return (int)textBox.GetValue(AttachedIntProperty);
  }
  
  public static void SetAttachedInt(TextBox textBox, int value) {
      textBox.SetValue(AttachedIntProperty, value);
  }
  
  public static readonly DependencyProperty AttachedIntProperty = 
     DependencyProperty.RegisterAttached("AttachedInt", typeof(int), 
     typeof(Ownerclass), new UIPropertyMetadata(0));
  </code></pre>
  
  <p>My question is, because this leads to an inconsistence, because GetValue and SetValue could be used anymore for any type and also in markup is no possibility to limit the attachement.</p>
  
  <p>What I did formerly was that I added an exception into the PropertyChanged handler and raised there an exception that only the types xy are allowed.</p>
  
  <p>What do you think?</p>