开发者

Silverlight 4 - How to set Custom Attached Dependency property in Style?

开发者 https://www.devze.com 2023-03-06 02:26 出处:网络
I have the following in my root ResourceDictionary. The Foreground = Red part works, but the custom attached dependency property does not get set.

I have the following in my root ResourceDictionary. The Foreground = Red part works, but the custom attached dependency property does not get set.

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).

<Style TargetType="TextBox">
    <Setter Property="controls:TextBoxContextMenu.HasContextMenu" Value="True" />
    <Setter Property="Foreground" Value="Red" />
</Style>


/// <summary>
    /// Gets the value of the HasContextMenu attached property for a specified TextBox.
    /// </summary>
    /// <param name="element">The TextBox from which the property value is read.</param>
    /// <returns>The HasContextMenu property value for the TextBox.</returns>
    public static bool GetHasContextMenu(TextBox element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }
        return (bool)element.GetValue(HasContextMenuProperty);
    }

    /// <summary>
    /// Sets the value of the HasContextMenu attached property to a specified TextBox.
    /// </summary>
    /// <param name="element">The TextBox to which the attached property is written.</param>
    /// <param name="value">The needed HasContextMenu value.</param>
    public static void SetHasContextMenu(TextBox element, bool value)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }
        element.SetValue(HasContextMenuProperty, value);
    }

    /// <summary>
    /// Identifies the HasContextMenu dependency property.
    /// </summary>
    public static readonly DependencyProperty HasContextMenuProperty =
        DependencyProperty.RegisterAttached(
            "HasContextMenu",
            typeof(bool),
            typeof(TextBox),
            new PropertyMetadata(false, OnHasContextMenuPropertyChanged));

    /// <summary>
    /// HasContextMenuProperty property changed handler.
    /// </summary>
    /// <param name="d">TextBoxContextMenu that changed its HasContextMenu.</param>
    /// <param name="e">Event arguments.</param>
    private static void OnHasContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ 
    // code
}

I should add that the attached dependency property is defined in a class which inherit开发者_Go百科s from RadContextMenu, which is a DependencyObject (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)


I figured it out. It was indeed due to having the attached property defined in the class I have.

To fix it, i created a new class called TextBoxContextMenuService and put the code in there instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消