所以,假设我想在 ViewModel 中发生某些逻辑时播放 Storyboard 动画。但是 StoryBoard 依赖于 View,而我没有从 ViewModel 中引用 View。那我怎么去玩 Storyboard呢?
最佳答案
您可以使用数据触发器在 View 中启动动画。
是这样的:
...<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value.Name}" Padding="5">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Value.StartAnimation}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard
Storyboard.TargetProperty="FontSize"
Duration="0:0:0.5">
<DoubleAnimation From="10" To="30" AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
...
关于c# - WPF:如何在 MVVM 中播放 Storyboard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4405606/