之后使用该属性的依赖属性?
我正在尝试设置一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没错,您不能使用 SetValue 引入任何逻辑。相反,您应该使用 PropertyValueChanged处理程序。像这样的东西:
That's correct, you cannot use SetValue to introduce any logic. Instead, you should use a PropertyValueChanged handler. Something like:
添加 DependencyPropertyChangedEventHandler 并将代码从 setter 移至此处:
Add a DependencyPropertyChangedEventHandler and move the code from your setter there: