title: Why should I use an attached property instead of a regular dependency property?
body: <p>I just discovered than I can do the following:</p>
  
  <pre><code>var button = new Button();
  button.SetValue(TextBlock.TextProperty, "text");
  var text = (string)button.GetValue(TextBlock.TextProperty); // text is "text" 
  </code></pre>
  
  <p>While the above example is a bit unrealistic, it does show that I can attach a regular dependency property onto another object. It doesn't have to be a an attached property (<code>TextBlock.TextProperty</code> is not registerd with <code>DependencyProperty.RegisterAttached()</code>.</p>
  
  <p>This bares the questions why are there attached properties in the first place? The only difference I can see for now ist that I can't attach regular dependency properties in XAML. But that's about it. Are there any other differences?</p>
  
  <p><strong><em>Update:</em></strong></p>
  
  <p>To make it more clear, the below code works and looks pretty close to an attached property from the end users perspective:</p>
  
  <pre><code>public static class AttachedPropertyDeclarer
  {
      public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
          "Text",
          typeof(string),
          typeof(Button), 
          new PropertyMetadata(default(string),OnTextChanged));
  
      private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          // do something when text changed
      }
  }
  
  ...
  button.SetValue(AttachedPropertyDeclarer.TextProperty, "text");
  var text = (string)button.GetValue(AttachedPropertyDeclarer.TextProperty);
  </code></pre>
  
  <p>Compare this to the attached property way:</p>
  
  <pre><code>public static class AttachedPropertyDeclarer
  {
      public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
          "Text",
          typeof(string),
          typeof(AttachedPropertyDeclarer),
          new PropertyMetadata(default(string),OnTextChanged));
  
      private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          // do something when text changed
      }
  }
  </code></pre>
  
  <p>The only effective differnce to an attached property here is that I have to declare the owner of type <code>Button</code> whereas in a attached property it would usually be <code>AttachedPropertyDeclarer</code>. But this only needs to be done if I need a changed event handler (i.e. <code>OnTextChanged</code>). </p>