开发者

Dependency Properties that use the property afterwards?

开发者 https://www.devze.com 2023-03-24 06:48 出处:网络
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 disp

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:

public Sprite Sprite
{
    get { return (Quiz.Sprite)GetValue(SpriteProperty); }
    set { SetValue(SpriteProperty, value); }
}

private static void OnSpritPropertyValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
    var control = d as YourControl;
    var sprite = e.NewValue as Quiz.Sprite;

    control.spriteBrush.ImageSource = new BitmapImage(new Uri("/Project;component/" + sprite.spriteSheet, UriKind.RelativeOrAbsolute));

    control.spriteTransform.TranslateX = -558;
    control.spriteTransform.TranslateY = 0;
}

public static DependencyProperty SpriteProperty = DependencyProperty.Register(
   "Sprite", typeof(Sprite), typeof(spriteView),
    new PropertyMetadata(new Quiz.Sprite() { spriteSheet = "wp7_buttons.png" }, OnSpritPropertyValueChanged));


Add a DependencyPropertyChangedEventHandler and move the code from your setter there:

public Sprite Sprite
{
    get { return (Quiz.Sprite)GetValue(SpriteProperty); }
    set { SetValue(SpriteProperty, value); }
}

public static DependencyProperty SpriteProperty = DependencyProperty.Register(
"Sprite", typeof(Sprite), typeof(SpriteView), new PropertyMetadata(new Quiz.Sprite() { spriteSheet = "wp7_buttons.png" },
SpriteChanged));

private static void SpriteChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    SpriteView view = sender as SpriteView;
    Sprite sprite = e.NewValue as Sprite;
    if (view != null && sprite != null)
    {
        view.spriteBrush.ImageSource = new BitmapImage(new Uri("/Project;component/" + sprite.spriteSheet, UriKind.RelativeOrAbsolute));
        view.spriteTransform.TranslateX = -558;
        view.spriteTransform.TranslateY = 0;
    }
}
0

精彩评论

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