如何从实现它的控件启动在样式中定义的动画?
我在名为“Error”的样式中创建了一个状态,并在故事板中附加了动画。
我希望能够在满足特定条件时从使用该样式的控件启动该动画。
例如,当我的 View 的 DataContext 中的属性设置为某个值时。
我究竟如何启动在控件的 Style 中定义的故事板?
I created a state in a Style named "Error" with an attached animation in a storyboard.
I would like to be able to launch that animation from a Control that uses that Style, whenever a certain condition is met.
For example, when a property in my View's DataContext is set to a certain value.
How exactly can I launch the storyboard defined in the control's Style ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在控件后面的代码中,只需执行以下操作:
如果您的 Storyboard 未设置为资源但嵌入到您的样式中,请将其声明为资源并在您的样式中引用它。
MSDN 文档:故事板
In the code behind of the control just do
If your Storyboard is not set as a resource but is embedded in your style, declare it as a resource and reference it in your style.
MSDN documentation: Storyboard
以下是使用触发器从 XAML 执行此操作的方法:
定义了上面的样式后,您只需告诉按钮要使用什么样式:
Here it is how you do it from XAML using Triggers:
After you have defined the style like above you simply need to tell the button what style to use:
我最终创建了一个类,它将保存所有控件的验证依赖属性:
在此之后,我修改了样式以根据使用该样式的控件的该属性的值启动动画:
我最终绑定了依赖属性在使用模型中值的样式的控件上(通过视图模型):
I ended up creating a class that will hold all of my controls' validation dependency properties:
Following this, I modified the style to launch the animation depending on the value of that property for the control that uses the style:
I finally bound the Dependency Property on the Control that uses the Style to the value in the Model (via the View-Model):