I’m trying to set up a dependency property which is of an object called a Sprite, a sprite contains a string of a spriteSheet, so i want to be able to pass a Sprite and then that data is used to display an image in my control.
This is my attempt as it stands
public Sprite Sprite
{
get { return (Quiz.Sprite)GetValue(SpriteProperty); }
set {
spriteBrush.ImageSource = new BitmapImage(new Uri("/Project;component/" + value.spriteSheet, UriKind.RelativeOrAbsolute));
spriteTransform.TranslateX = -558;
spriteTransform.TranslateY = 0;
SetValue(SpriteProperty, value);
}
}
public static DependencyProperty SpriteProperty = DependencyProperty.Register(
"Sprite", typeof(Sprite), typeof(spriteView), new PropertyMetadata(new Quiz.Sprite() { spriteSheet = "wp7_buttons.png" }));
}
How do I get that portion of the set method to run, as i’ve read in the Dependency Property docs that SetValue and GetValue might get called directly when binding.
That’s correct, you cannot use SetValue to introduce any logic. Instead, you should use a PropertyValueChanged handler. Something like: