WPF/Silverlight:如何在 MVVM 中数据触发故事板动画?
我有一个名为 IsLoginWrong 的布尔属性,如果 IsLoginWrong 为 true,我想播放故事板动画。 (IsLoginWrong 执行 OnPropertyChanged 事件,所以我知道绑定没问题)但是我在语法上遇到了困难。这甚至可能不对,但我认为数据触发器只能存在于样式中...
<UserControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsLoginWrong}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource LoginWrong}"/>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
但这会引发异常“样式中的故事板树无法指定 TargetName”...因为样式无法具体引用项目...太棒了。那么我该如何做我想做的事情呢? (如果 mvvm 中的布尔值发生变化,则播放动画)
谢谢
I have a boolean property called IsLoginWrong, I want to then play a storyboard animation if the IsLoginWrong is true. (IsLoginWrong does an OnPropertyChanged event, so I know the binding is ok) But I'm having a hard time with the syntax. This might not even be right, but I think datatriggers can only live in Styles...
<UserControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsLoginWrong}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource LoginWrong}"/>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
But this throws an exception "A storyboard tree in a Style cannot specify a TargetName"... beause styles canno refer to items specifically.. awesome. so how do I do what I'm trying to do? (play animation if a boolean changes in mvvm)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在样式中,您不能引用故事板名称。所以我让它工作的方法是将你的故事板推入实际的样式中:
现在 DataTriggers 可以放入样式或控件模板中,因此可能有更好的方法使用控件模板来做到这一点。但这是我暂时想到的。
Within a style you cannot refer to a storyboard name. So the way I got it to work is to shove your storyboard within the actual style:
Now
DataTriggers
can either be put into styles or control templates, so there might be a nicer way to do this with control templates. but this is what I came up with for the time being.一种选择是使用 VisualStateManager 启动故事板。文章位于http://blogs.infosupport.com/blogs/alexb/archive/2010/04/02/silverlight-4-using-the-visualstatemanager-for-state-animations-with-mvvm。 aspx 解释了如何使用附加属性从视图模型控制 VisualStateManager 的当前状态。
One option would be to start the storyboard using the VisualStateManager. The article at http://blogs.infosupport.com/blogs/alexb/archive/2010/04/02/silverlight-4-using-the-visualstatemanager-for-state-animations-with-mvvm.aspx explains how to control the current state of the VisualStateManager from the view model using an attached property.