title: How to use the DependencyProperties of a Blend Behavior during its initialization?
body: <p>Here I have a simple Blend Behavior. It has a single <code>DependencyProperty</code> which I want to do something with during initialization in <code>OnAttached</code>. But I can't because the behavior appears to be attached before it is even initialized!</p>
  
  <p>Usage:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Button&gt;
      &lt;e:Interaction.Behaviors&gt;
          &lt;local:TestBehavior Test1="{Binding ValueOnViewModel}"/&gt;
      &lt;/e:Interaction.Behaviors&gt;
  &lt;Button&gt;
  </code></pre>
  
  <p>Definition:</p>
  
  
  
  <pre><code>class TestBehavior : Behavior&lt;FrameworkObject&gt;
  {
      public static readonly DependencyProperty Test1Property
          = DependencyProperty.Register("Test1", typeof(int), typeof(TestBehavior),
          new PropertyMetadata(OnTest1Property_Changed));
  
      private static void OnTest1Property_Changed(DependencyObject sender,
          DependencyPropertyChangedEventArgs args)
      {
          // This gets called _after_ OnAttached!
      }
  
      public int Test1
      {
          get { return (int)GetValue(Test1Property); }
          set { SetValue(Test1Property, value); }
      }
  
      protected override void OnAttached()
      {
          base.OnAttached();
  
          // No matter what Test1Property is bound to in XAML 'a' will be default
          // value because as this point the Behavior is attached, but not
          // initialized!
          int a = Test1;
      }
  }
  </code></pre>
  
  <p>This strikes me as really bizarre. In this simple case I can get around the issue by performing my initialization in OnTest1Property_Changed instead of in OnAttached, (albeit with the mild inconvenience of being in a static context rather than an instance context).</p>
  
  <p>However, what if I have a rather less trivial Behavior which has multiple properties? In some usages of the behavior all the DP's might be explicitly set, whereas in other cases only some of the DP's might be set while using the default values of the DP's that are not explicitly set. I can handle the change event for all of the DP's the behavior defines, but I have no way of knowing which DP's the client has set explicitly in XAML, so I have no way of knowing with change notifications have to have occurred before initialization is complete.</p>
  
  <p>So how in that non-trivial case can I possibly know when initialization of the behavior is complete? This seems like such a glaring weakness that I can only assume I've missed something obvious.</p>
  
  <p>[Update]</p>
  
  <p>Using Sorskoot's suggestion I knocked up this simple base class which I can use as the base for my Behaviors instead.</p>
  
  
  
  <pre><code>public class BehaviorBase&lt;T&gt; : Behavior&lt;T&gt; where T : FrameworkElement
  {
      protected override void OnAttached()
      {
          base.OnAttached();
          AssociatedObject.Loaded += AssociatedObject_Loaded;
      }
  
      protected override void OnDetaching()
      {
          base.OnDetaching();
          AssociatedObject.Loaded -= AssociatedObject_Loaded;
      }
  
      protected virtual void OnLoaded()
      {
  
      }
  
      private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
      {
          OnLoaded();
      }
  }
  </code></pre>