Style vs inline property setting in silverlight with a custom control

108 Views Asked by At

I have a custom silverlight control that is pretty much a glorified text box. I have some properties in it I need to be able to set from the XAML in silverlight, so for each property I have created something like this:

public bool UseCustomTooltips
    {
        get { return _useCustomTooltips; }
        set
        {
            _useCustomTooltips = value;
            DoSomeOtherStuff();
        }
    }

public static readonly DependencyProperty UseCustomTooltipsProperty = DependencyProperty.Register("UseCustomTooltips",
        typeof(bool), typeof(MyControl), new PropertyMetadata(false, PropertyChangedCallback));

In the XAML I can create the control and specify a value for that property like this:

<Controls:MyControl UseCustomTooltips="True" />

The control's state is update, my callback is hit, and all is as it should be. However, when I try to specify that state from a style instead of in each instance of the control like this:

<Style x:Key="MyControl" TargetType="Controls:MyControl">
    <Setter Property="Foreground" Value="Yellow"/>
    <Setter Property="UseCustomTooltips" Value="True"/>
</Style>

<Controls:MyControl Style="{StaticResource MyControl}" />

my custom properties are never set and my callback is never hit even though the inherited property Foreground takes on the value specified in the style. This leads me to assume there is something wrong with the way my dependency property is wired up but everything I've seen so far online tells me I have it correct.

Can anyone point me in the right direction? Thanks.

1

There are 1 best solutions below

2
On BEST ANSWER

That is not the correct way to register a dependency property.

    public bool UseCustomTooltips
    {
        get { return (bool)GetValue(UseCustomTooltipsProperty); }
        set { SetValue(UseCustomTooltipsProperty, value); }
    }

    public static readonly DependencyProperty UseCustomTooltipsProperty =
        DependencyProperty.Register("UseCustomTooltips", 
        typeof(bool),
        typeof(MyControl), 
        new PropertyMetadata(false, new PropertyChangedCallback(MyCallbackMethod)));

Use the propdp snippet, it really is a beautiful thing.