title: Error when binding a Dependency Property on a custom Behavior
body: <p>I am exploring the Silverlight attached behaviors mechanism in order to use the Model-View-ViewModel pattern within my Silverlight applications. To start with, I am trying to get a simple Hello World working, but I am completely stuck in an error for which I'm not able to find a solution.</p>
  
  <p>What I have right now is a page that just contains a button which should display a message when clicked. The click event is handled by using a class derived from Behavior, and the message is specified as a dependency property of the behavior itself. The problem comes when trying to bind the message property to a property on a viewmodel class used as the data context: I get an exeption in the call to <code>InitializeComponent</code> in the view.</p>
  
  <p>Here is all the code I'm using, as you can see it is rather simple. First the markup of the main page and the view it contains:</p>
  
  <p><br/>
  <strong>MyPage</strong></p>
  
  <pre><code>&lt;UserControl x:Class="MyExample.MyPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MyExample"
      &gt;
      &lt;Grid x:Name="LayoutRoot"&gt;
          &lt;local:MyView/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><br/>
  <strong>MyView</strong> (the TextBlock is there just to check that the binding syntax is correct)</p>
  
  <pre><code>&lt;UserControl x:Class="MyExample.MyView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
      xmlns:local="clr-namespace:MyExample"
      Width="400" Height="300"&gt;
      &lt;StackPanel Orientation="Vertical" x:Name="LayoutRoot" Background="White"&gt;
          &lt;StackPanel.Resources&gt;
              &lt;local:MyViewmodel x:Key="MyResource"/&gt;
          &lt;/StackPanel.Resources&gt;
          &lt;TextBlock Text="This button will display the following message:"/&gt;
          &lt;TextBlock Text="{Binding MyMessage, Source={StaticResource MyResource}}" FontStyle="Italic"/&gt;
          &lt;Button x:Name="MyButton" Content="Click me!"&gt;
              &lt;i:Interaction.Behaviors&gt;
                  &lt;local:MyBehavior Message="{Binding MyMessage, Source={StaticResource MyResource}}"/&gt;
              &lt;/i:Interaction.Behaviors&gt;
          &lt;/Button&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><br/>
  Now the code, there are two classes: one for the behavior and another one for the viewmodel:</p>
  
  <p><strong>MyViewmodel</strong></p>
  
  <pre><code>public class MyViewmodel
  {
      public string MyMessage
      {
          get { return "Hello, world!"; }
      }
  }
  </code></pre>
  
  <p><br/>
  <strong>MyBehavior</strong></p>
  
  <pre><code>public class MyBehavior : Behavior&lt;Button&gt;
  {
      public static readonly DependencyProperty MessageProperty =
          DependencyProperty.Register("Message",
          typeof(string), typeof(MyBehavior),
          new PropertyMetadata("(no message)"));
  
      public string Message
      {
          get { return (string)GetValue(MessageProperty); }
          set { SetValue(MessageProperty, value); }
      }
  
      protected override void OnAttached()
      {
          base.OnAttached();
          AssociatedObject.Click += new RoutedEventHandler(AssociatedObject_Click);
      }
  
      protected override void OnDetaching()
      {
          base.OnDetaching();
          AssociatedObject.Click -= new RoutedEventHandler(AssociatedObject_Click);
      }
  
      void AssociatedObject_Click(object sender, RoutedEventArgs e)
      {
          MessageBox.Show(Message);
      }
  }
  </code></pre>
  
  <p><br/></p>
  
  <p>Simple enough, but this code throws an <code>AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 15 Position: 43]</code> (right at the start of the value being set for the Message property) exception when ran. I'm sure that I'm missing something, but what?</p>
  
  <p>Additional information: if I remove the binding from the Message property on MyBehavior (that is, if I set its value to any static string), it works fine. Also, I'm targeting silverlight 3 RTW.</p>
  
  <p>Thanks a lot!</p>
  
  <p><br/>
  <strong>UPDATE</strong></p>
  
  <p>It seems that unlike WPF, Silverlight does not support data binding on any object deriving from DependencyObject, but only on objects deriving from FrameworkElement. This is no the case for Behavior, hence binding does not work.</p>
  
  <p>I have found a workaround <a href="http://www.sharpgis.net/post/2009/05/03/Using-surrogate-binders-in-Silverlight.aspx" rel="noreferrer">here</a>, in the form of something named surrogate binders. Basically you specify the element and property to be binded, as well as the value, as attributes of the FrameworkElement containing the non-FrameworkElement object.</p>
  
  <p><br/>
  <strong>UPDATE 2</strong></p>
  
  <p>The surrogate binder does not work when the FrameworkElement contains an Interaction.Behaviors sub-element.</p>
  
  <p>I have found another solution <a href="http://forums.silverlight.net/forums/p/14579/49805.aspx" rel="noreferrer">here</a>, and this one seems to work. This time, the trick used is a <em>DeepSetter</em>. You define one of such setters as a static resource on the containing StackPanel, and then reference the resource from the behavior. So in my example, we should expand the StackPanel resources section as follows:</p>
  
  <pre><code>&lt;StackPanel.Resources&gt;
      &lt;local:MyViewmodel x:Key="MyResource"/&gt;
      &lt;local:DeepSetter 
          x:Key="MyBehaviorSetter"
          TargetProperty="Message"
          BindingExport="{Binding MyMessage, Source={StaticResource MyResource}}"/&gt;
  &lt;/StackPanel.Resources&gt;
  </code></pre>
  
  <p>...and modify the button's behavior declaration as follows:</p>
  
  <pre><code>&lt;local:MyBehavior local:DeepSetter.BindingImport="{StaticResource MyBehaviorSetter}"/&gt;
  </code></pre>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>Good news: data binding for any DependecyObject will be available on Silverlight 4: <a href="http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#dobind" rel="noreferrer">http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#dobind</a></p>