Joseph Le Brech
Joseph Le Brech

Reputation: 6653

Outside a datatemplate On..PropertyValueChanged doesn't run

I have a custom control which uses On(propertyname)ValueChanged to read items from a dictionary and set up the parameters for that control.

I would also like to use that control as a stand alone and not just a databound control.

So how come OnPropertyValueChanged only works in a dataset?

Do Dependency properties only work from the xaml, does that mean i will have to bind from properties in the container class? (may have answered my own question)

in my mainpage.xaml

    <local:spriteToggleButton x:Name="testButton" HorizontalAlignment="Center" Text="{Binding testString, ElementName=mainPage}" Correct="true" Margin="93,561,93,63" Grid.Row="1" Sprites="{Binding testSprites, ElementName=mainPage}" />

in mainpage.xaml.cs

    testSprites.Add("idle", idlesprite); // a dictionary of a custom sprite object
    testSprites.Add("highlighted", highlightedsprite);
    testSprites.Add("selected", selectedsprite);

    testString = "this is a test"; // this property is picked up by the binding.

when i add sprites from the binding it runs the dependency property changed callback but the properties inside the spriteToggleButton class are not updating when the control is used standalone

this is my dependency property changed callback

    private static void OnSpritesPropertyValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as spriteToggleButton;
        var sprites = e.NewValue as Dictionary<string, Quiz.Sprite>;

        control.idleSprite = sprites["idle"];
        control.selectedSprite = sprites["selected"];
        control.highlightedSprite = sprites["highlighted"];
    }

this is inside my spriteToggleButton

 <local:spriteView x:Name="Idle" Width="294" Height="57" HorizontalAlignment="Center" Sprite="{Binding idleSprite, ElementName=toggleSpriteControl}"  />
 ...

Sprite is also a dependency property in that control

Upvotes: 1

Views: 91

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189505

I very much suspect that you haven't implemented idleSprite, selectedSprite and highlightedSprite of the spriteToggleButton class as dependency properties. Do that and it should start working.

For what its worth it appears you are implementing spriteToggleButton using a UserControl, I would instead derive from ToggleButton and replace the default template.

Upvotes: 1

Related Questions