title: How Do I Set A Style On A UserControl Dependency Property?
body: <p>I have created a UserControl with a dependency property named HighlightedBorderBrush.
  I registered the dependency property as follows:</p>
  
  <pre><code>    public static readonly DependencyProperty HighlightedBorderBrushProperty =
      DependencyProperty.Register("Highlighted_BorderBrush", typeof(Brush),
                                  typeof(MyUserControl),
                                  new FrameworkPropertyMetadata(Brushes.Black));
  
      public Brush HighlightedBorderBrush
      {
          get { return (Brush)GetValue(HighlightedBorderBrushProperty); }
          set {SetValue(HighlightedBorderBrushProperty, value); }
      }
  </code></pre>
  
  <p>In the code where I use the UserControl I am able to set the HighlightedBorderBrush property directly in XAML as follows:</p>
  
  <pre><code>    &lt;local:MyUserControl HighlightedBorderBrush="Red" /&gt;
  </code></pre>
  
  <p>This works fine.  However, when I attempt to use a style I get an error:  "Exception has been thrown by the target of an invocation."</p>
  
  <p>Here is the code where I attempt to use a style:</p>
  
  <pre><code>    &lt;!-- Define a style for the user control --&gt;
      &lt;Window.Resources&gt;
          &lt;Style x:Key="UserControlStyle" TargetType="{x:Type local:MyUserControl}"&gt;
              &lt;Setter Property="HighlightedBorderBrush" Value="Red"/&gt;
          &lt;/Style
      &lt;/Window.Resources&gt;
  
      .
      .
      .
  
      &lt;!-- Use a Style to set the HighlightedBorderBrush property --&gt;
      &lt;local:MyUserControl Style="{StaticResource UserControlStyle}" /&gt;
  </code></pre>
  
  <p>I'm guessing that I am missing something obvious but cannot figure it out.  Help!</p>