title: Silverlight 4 - How to set Custom Attached Dependency property in Style?
body: <p>I have the following in my root ResourceDictionary. The <code>Foreground = Red</code> part works, but the custom attached dependency property does not get set.</p>
  
  <p>I can set it manually through code, but I obviously want to avoid having to set it for every textbox. Does this work in Silverlight? I have seen some posts about doing it in WPF, and my code looks right (to me).</p>
  
  <pre><code>&lt;Style TargetType="TextBox"&gt;
      &lt;Setter Property="controls:TextBoxContextMenu.HasContextMenu" Value="True" /&gt;
      &lt;Setter Property="Foreground" Value="Red" /&gt;
  &lt;/Style&gt;
  
  
  /// &lt;summary&gt;
      /// Gets the value of the HasContextMenu attached property for a specified TextBox.
      /// &lt;/summary&gt;
      /// &lt;param name="element"&gt;The TextBox from which the property value is read.&lt;/param&gt;
      /// &lt;returns&gt;The HasContextMenu property value for the TextBox.&lt;/returns&gt;
      public static bool GetHasContextMenu(TextBox element)
      {
          if (element == null)
          {
              throw new ArgumentNullException("element");
          }
          return (bool)element.GetValue(HasContextMenuProperty);
      }
  
      /// &lt;summary&gt;
      /// Sets the value of the HasContextMenu attached property to a specified TextBox.
      /// &lt;/summary&gt;
      /// &lt;param name="element"&gt;The TextBox to which the attached property is written.&lt;/param&gt;
      /// &lt;param name="value"&gt;The needed HasContextMenu value.&lt;/param&gt;
      public static void SetHasContextMenu(TextBox element, bool value)
      {
          if (element == null)
          {
              throw new ArgumentNullException("element");
          }
          element.SetValue(HasContextMenuProperty, value);
      }
  
      /// &lt;summary&gt;
      /// Identifies the HasContextMenu dependency property.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty HasContextMenuProperty =
          DependencyProperty.RegisterAttached(
              "HasContextMenu",
              typeof(bool),
              typeof(TextBox),
              new PropertyMetadata(false, OnHasContextMenuPropertyChanged));
  
      /// &lt;summary&gt;
      /// HasContextMenuProperty property changed handler.
      /// &lt;/summary&gt;
      /// &lt;param name="d"&gt;TextBoxContextMenu that changed its HasContextMenu.&lt;/param&gt;
      /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
      private static void OnHasContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  { 
      // code
  }
  </code></pre>
  
  <p>I should add that the attached dependency property is defined in a class which inherits from <code>RadContextMenu</code>, which is a <code>DependencyObject</code> (I have been reading and somewhere it is suggested that this can't work if the attached property is defined in such a class, but this seems strange)</p>