title: How do I get attached properties to work in code-behind
body: <p>I'm trying to get attached properties to work in code behind, but I'm clearly missing something. As I understand it result should be "test", but it is string.Empty. </p>
  
  <p>The LogicalTreeHelper states that <code>child</code> is the child of <code>parent</code>, so the tree is set up correctly.</p>
  
  <p>Any suggestions?</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          var parent = new TestParent();
          var child = new Child();
          parent.AddLogicalChild(child);
          parent.SetValue(TestParent.TestProperty, "test");
  
          var result = child.GetValue(TestParent.TestProperty); // Returns ""
  
          InitializeComponent();
      }
  }
  
  
  class TestParent : FrameworkElement
  {
      public static readonly DependencyProperty TestProperty = 
          DependencyProperty.RegisterAttached("Test", typeof(string), typeof(TestParent), 
          new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits));
  
      public void AddLogicalChild(FrameworkElement element)
      {
          base.AddLogicalChild(element);
      }
  }
  
  class Child : FrameworkElement
  {
  
  }
  </code></pre>