WPF:如何在MVVM中播放故事板?

for*_*yez 8 c# wpf mvvm

所以,如果在ViewModel中发生某些逻辑,我想播放故事板动画.但是StoryBoard存在于View中,我没有从ViewModel引用View.那我怎么去玩故事板呢?

Tho*_*son 7

您可以使用数据触发器在视图中启动动画.

像这样的东西:

    ...<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>
...
Run Code Online (Sandbox Code Playgroud)