title: Programmatically binding an attached property in WPF
body: <p>I'm using <a href="http://www.ageektrapped.com/blog/the-missing-net-4-cue-banner-in-wpf-i-mean-watermark-in-wpf/" rel="nofollow noreferrer">Jason Kemp's cool <code>CueBanner</code> class</a> for "watermarking" text boxes in WPF:</p>
  
  <pre><code>&lt;TextBox 
      hpext:CueBanner.Content="Find Text"
      hpext:CueBanner.Padding="2,6,0,0"
      /&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Y2Uca.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y2Uca.png" alt="Don&#39;t tell Ayn Rand"></a></p>
  
  <p>Jason's code creates a <code>ContentPresenter</code> programmatically, sets its content to the <code>CueBanner.Content</code>, adds the <code>ContentPresenter</code> to an <code>Adorner</code> subclass, and displays it when appropriate. </p>
  
  <p>His code infers the <code>ContentPresenter</code> from padding and margin of the adorned control, but I added <code>hpext:CueBanner.Padding</code> instead so I could set it explicitly to a value I like better. </p>
  
  <pre><code>contentPresenter.Margin = CueBanner.GetPadding(adornedElement);
  </code></pre>
  
  <p>So far so good. </p>
  
  <p>Here's the trouble: It works but I'd like to bind it instead. </p>
  
  <pre><code>var binding = new Binding("hpext:CueBanner.Padding") { Source = adornedElement };
  
  BindingOperations.SetBinding(contentPresenter, ContentPresenter.MarginProperty, binding);
  </code></pre>
  
  <p><strong>But what do I use for a path for the source property?</strong> <code>"hpext:CueBanner.Padding"</code> doesn't work and it shouldn't work; that namespace doesn't exist there. No joy with <code>"CueBanner.Padding"</code> and <code>"(CueBanner.Padding)"</code>. <code>"MyProject.Extensions.CueBanner.Padding"</code> doesn't work either and I can't see why it ought to. </p>
  
  <p>I can't see anything that looks helpful in <a href="https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms742451(v=vs.100)" rel="nofollow noreferrer"><code>PropertyPath</code></a>, but when I read that my mind starts to go a little blank. </p>
  
  <p>I should be just doing this in XAML, shouldn't I? </p>