之后使用该属性的依赖属性?

发布于 2024-11-27 05:28:03 字数 838 浏览 1 评论 0原文

我正在尝试设置一个名为 Sprite 的对象的依赖属性,一个 sprite 包含 spriteSheet 的字符串,所以我希望能够传递一个 Sprite,然后使用该数据在我的窗口中显示图像控制。

这是我的尝试,因为

    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" }));
}

我如何让 set 方法的该部分运行,正如我在依赖属性文档中读到的那样,SetValue 和 GetValue 在绑定时可能会被直接调用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-12-04 05:28:03

没错,您不能使用 SetValue 引入任何逻辑。相反,您应该使用 PropertyValueChanged处理程序。像这样的东西:

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

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));
各自安好 2024-12-04 05:28:03

添加 DependencyPropertyChangedEventHandler 并将代码从 setter 移至此处:

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文