title: Change Source of Binding for attached property
body: <p>If I use binding extension in attached property</p>
  
  <pre><code>&lt;TextBlock local:MyBehavior.View="{Binding A}" /&gt; &lt;!-- or B --&gt;
  </code></pre>
  
  <p>How can set value of <code>A</code> (or <code>B</code>) property of ViewModel from that attached behavior?</p>
  
  <p>I do not understand:</p>
  
  <ol>
  <li>Which type to use for attached property? <code>Binding</code>? <code>BindingBase</code>? <code>BindingExpressionBase</code>? <code>object</code>?</li>
  <li>Shall I set the value immediately? Shall I wait for some event? Shall I use another dependency property to set it's value, then bind to <code>SomeProperty</code> and let binding do the job once <code>DataContext</code> is set?</li>
  </ol>
  
  <p>My failed attempt is <a href="https://stackoverflow.com/q/41612129/1997232">here</a>, for convenience I copied it below:</p>
  
  <pre><code>public class MyBehavior
  {
      public static BindingBase GetView(DependencyObject obj) =&gt; (BindingBase)obj.GetValue(ViewProperty);
      public static void SetView(DependencyObject obj, BindingBase value) =&gt; obj.SetValue(ViewProperty, value);
      public static readonly DependencyProperty ViewProperty =
          DependencyProperty.RegisterAttached("View", typeof(BindingBase), typeof(MyBehavior), new PropertyMetadata(null, (d, e) =&gt;
          {
              var element = d as FrameworkElement;
              if (element == null)
                  throw new ArgumentException("Only used with FrameworkElement");
              element.Loaded += (s, a) =&gt; GetView(element); // &lt;&lt;
          }));
  }
  </code></pre>
  
  <p>I am not sure what to do at marked line to set value of given by binding property:</p>
  
  <pre><code>public class ViewModel
  {
      public object A { get; set; }
      public object B { get; set; }
  }
  </code></pre>