Setting a ViewModel boolean property inside a storyboard

﹥>﹥吖頭↗ 提交于 2021-02-07 17:31:06

问题


I have a storyboard that's triggered by a property in the ViewModel, this trigger the animation to start. But how do I set the "Saved" property back to False when it is done with the animation (to trigger the ExitAction.)

<Style TargetType="TextBlock" x:Key="FadeInOut">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Saved}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" From="0" To="1" />
                            <!-- set "Saved" to false when done -->
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" From="1" To="0"  />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.ExitActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>

回答1:


If you can navigate to your property i would recommend a BooleanAnimationUsingKeyFrames with a discrete frame at your end-time.

<BooleanAnimationUsingKeyFrames
            Storyboard.TargetProperty="DataContext.Saved"
            FillBehavior="HoldEnd">
            <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:1" />
</BooleanAnimationUsingKeyFrames>

Since you use a binding to Saved i assume using the DataContext should work.




回答2:


You can use the ObjectAnimationUsingKeyFrames to set the property. I'm not quite sure if you could use other animations but this is the one I recently used.

    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Saved">
        <DiscreteObjectKeyFrame KeyTime="0">
            <DiscreteObjectKeyFrame.Value>
                <system:Boolean>False</system:Boolean>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>


来源:https://stackoverflow.com/questions/5647452/setting-a-viewmodel-boolean-property-inside-a-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!